我有一个带有内联详细信息模型的父模型。保存内联模型详细信息时,如何在保存整个记录(即父项及其详细信息)之前添加新的详细信息记录?示例:models.py:
class Voucher(models.Model):
date = models.DateField(default=datetime.datetime.now().date())
number = models.CharField(max_length=20,null=True,blank=True)
class VoucherDetail(models.Model):
voucher = models.ForeignKey(Voucher,blank=True,null=True)
amount = models.DecimalField(decimal_places=2,max_digits=9,default=0,
blank=True,null=True)
narration = models.CharField(max_length=100,null=True,blank=True)
total_amount = models.DecimalField(decimal_places=2,max_digits=9,default=0,
blank=True,null=True)
在保存凭证之前,我想添加表单集的总计并在凭证明细中创建另一个条目
有一些叙述和总金额
我需要同样的完整代码示例。预先使用Django 1.6.5,Thx
在父模型管理员中使用以下方法并进行必要的计算以实现相同的目的:
def save_model(self, request, obj, form, change):
for d in obj.voucherdetail_set.all():
if d.value:
amt_exists = True
pass
else:
amt_exists =False
if amt_exists is False:
try:
dr_detail = VoucherDetail()
dr_detail.voucher = Voucher.objects.get(pk=obj.pk)
dr_detail.narration = "Client remarks"
dr_detail......
dr_detail.save()
except Exception,e:
print e
obj.save()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句