0

to count about multiply model with another model in foreignkey but in different app, here the penjualan app and detail_penjualan_obat model :

class detail_penjualan_obat(models.Model):

    kd_penjualan_detail = models.ForeignKey(penjualan_obat)
    kd_obat_detail = models.ForeignKey(obat)
    jumlah_jual = models.IntegerField()
    total_harga_perobat = MoneyField(max_digits=10, decimal_places=2, default_currency='IDR')


    def __unicode__(self):
        return self.total_harga_perobat

and the obat app model :

class obat(models.Model):

    jenis_obat = {

        ('obat bebas','Obat Bebas'),
        ('obat bebas terbatas','Obat Bebas Terbatas'),
        ('obat keras dan psikotropika','Obat Keras dan Psikotropika')
    }

    kd_obat = models.CharField(primary_key = True, default = '', max_length = 10, validators=[RegexValidator(r'^\d{1,10}$')])
    nama_obat = models.CharField(max_length = 15)
    tipe_obat = models.CharField(max_length = 35, choices = jenis_obat)
    harga_jual = MoneyField(max_digits=10, decimal_places=2, default_currency='IDR')

    def __unicode__(self):
        return self.kd_obat

and the view.py :

def data_penjualan_obat_detail(request):
    if request.method == 'POST':
        form_data = request.POST
        form = penjualan_detail_form(form_data)

        if form.is_valid():

            input_detail_penjualan = detail_penjualan_obat(

                kd_penjualan_detail = form.cleaned_data.get('kd_penjualan_detail'),
                kd_obat_detail =  form.cleaned_data.get('kd_obat_detail'),
                jumlah_jual = request.POST['jumlah_jual'],
                total_harga_perobat = form.cleaned_data.get('total_harga_perobat')

                )
            input_detail_penjualan.save()
            return redirect('/')
    else:
        form = penjualan_detail_form()

    return render(request, 'penjualan_detail.html',{'form':form})

here the function : total_harga_perobat = harga_jual * jumlah_jual

and that function how to be 'place and work' in the view ?

Sorry I have bad english but I hope you will understand what about my question, and solve my problem

EDIT :

here model of penjualan_obat :

class penjualan_obat(models.Model):

    kd_penjualan = models.CharField(primary_key = True, default = '', max_length = 10, validators=[RegexValidator(r'^\d{1,10}$')], unique = True)
    kd_costumer_obat = models.ForeignKey(costumer)
    tgl_penjualan = models.DateField(auto_now_add = True)
    total_penjualan = MoneyField(max_digits=10, decimal_places=2, default_currency='IDR')

    def __unicode__(self):
        return self.kd_penjualan

and the form penjualan_detail_form :

class penjualan_detail_form(ModelForm):
    class Meta:

        model = detail_penjualan_obat
        fields = ['kd_penjualan_detail','kd_obat_detail','jumlah_jual']
        labels = {

            'kd_penjualan_detail' : 'Kode Penjualan',
            'kd_obat_detail' : 'Kode Obat',
            'jumlah_penjualan' : 'Jumlah Penjualan',
        }

        error_messages = {

            'kd_penjualan_detail':{
                'required':'Anda harus memilih kode penjualan'
            },

            'kd_obat_detail':{
                'required':'Anda harus memilih kode obat'
            },

            'jumlah_penjualan':{
                'required':'Anda harus mengisi jumlah penjualan'
            },      
        }
  • give me your model of `penjualan_obat` and form `penjualan_detail_form` – binpy Feb 20 '17 at 17:59
  • here penjualan_obat model : https://github.com/Rifqi31/SI_Apotek/blob/master/costumer/models.py and penjualan_detail_form: https://github.com/Rifqi31/SI_Apotek/blob/master/costumer/forms.py – Rifqi Muttaqin Feb 20 '17 at 18:24

1 Answers1

0

CORRECTIONS

  1. I suggest you to change model class name with standard naming. an example class detail_penjualan_obat(models.Model) to class DetailPenjualanObat(models.Model)

  2. and I suggest you to not using default = '' to your unique field. This will be difficult whenever you migrating the databases.

  3. Why you implement to the form form.cleaned_data.get('total_harga_perobat'), if you actually want to save it with automatically? you should handle it at your backend.


I checked at your source code, and your penjualan_detail_form already instanced to the model of detail_penjualan_obat. perhaps like this below;

def data_penjualan_obat_detail(request):
    if request.method == 'POST':
        form = penjualan_detail_form(request.POST)
        if form.is_valid():
            initial = form.save(commit=False)
            initial.total_harga_perobat = initial.kd_obat_detail.harga_jual * initial.jumlah_jual
            initial.save()
            form.save()
            return redirect('/')
    else:
        form = penjualan_detail_form()

    return render(request, 'penjualan_detail.html', {'form': form})

Hope it can help..

UPDATE

You also can handle it in your models.py, an example:

class DetailPenjualanObat(models.Model):
    kd_penjualan_detail = models.ForeignKey(penjualan_obat)
    kd_obat_detail = models.ForeignKey(obat)
    jumlah_jual = models.IntegerField()
    total_harga_perobat = MoneyField(max_digits=10, decimal_places=2, default_currency='IDR')

    def save(self, *args, **kwargs):
        self.total_harga_perobat = self.kd_obat_detail.harga_jual * self.jumlah_jual
        super(DetailPenjualanObat, self).save(*args, **kwargs)

See also about pre save, like this answer

Community
  • 1
  • 1
binpy
  • 3,994
  • 3
  • 17
  • 54
  • if in the backend or in the model, how to count that ? – Rifqi Muttaqin Feb 21 '17 at 03:25
  • I already included it `initial.total_harga_perobat = initial.kd_obat_detail.harga_jual * initial.jumlah_jual`, `initial` is initial object for `detail_penjualan_obat` – binpy Feb 21 '17 at 07:32