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'
},
}