在外键中使用另一个模型来计算乘法模型,但在不同的应用程序中,这里是 penjualan app 和 detail_penjualan_obat模型:
SELECT t.myYear as Year, ifnull(COUNT(`tblrespond`.`patient_no`), 0) as Total
from (
select 2011 as myYear
union
select 2012
union
select 2013
union
select 2014
union
select 2015
union
select 2016
) t
LEFT JOIN `tblrespond` on t.myYear = `tblrespond`.YEAR(`date_incident`)
Inner Join `tblincident` On `tblincident`.`incident_no` = `tblrespond`.`incident_no`
Inner Join `tblpatients` On `tblpatients`.`patient_no` = `tblrespond`.`patient_no`
和 obat app 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
和view.py:
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
这里的功能: total_harga_perobat = harga_jual * jumlah_jual
该功能如何成为' 放置和工作'在视图中?
抱歉,我的英语不好,但我希望你能理解我的问题,并解决我的问题
编辑:
这里的penjualan_obat模型:
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})
和表单penjualan_detail_form:
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
答案 0 :(得分:0)
我建议您使用标准命名更改模型类名称。
示例appctrl
到class detail_penjualan_obat(models.Model)
我建议您不要将class DetailPenjualanObat(models.Model)
用于default = ''
字段。
迁移数据库时,这将很困难。
为什么要实施unique
表格,
如果你真的想自动保存它?你应该在你的后端处理它。
我查看了您的源代码和您的
form.cleaned_data.get('total_harga_perobat')
已经实例化为penjualan_detail_form
的模型。或许像下面这样;
detail_penjualan_obat
希望它可以帮助..
您也可以在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})
中处理它,例如:
models.py