Django inlineformset - 自定义保存方法
这是我的 models.py
class Invoices(models.Model):
...
sum_w_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_wo_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_discount = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_paid = models.DecimalField(max_digits=7, decimal_places=2, default=0)
...
class InvoiceItems(models.Model):
invoice = models.ForeignKey(Invoices)
quantity = models.DecimalField(max_digits=9, decimal_places=2)
unit = models.ForeignKey(StocklistUnits, verbose_name='Merska enota')
price = models.DecimalField(max_digits=9, decimal_places=2)
vat = models.DecimalField(max_digits=4, decimal_places=3)
discount = models.DecimalField(max_digits=3, decimal_places=1)
def save(self, **kwargs):
self.invoice.sum_w_vat += (self.price * self.quantity * self.vat) * self.discount
self.invoice.sum_wo_vat += (self.price * self.quantity) * self.discount
self.invoice.sum_discount += (self.price * self.quantity) * ( self.discount / 100 )
self.invoice.sum_vat += ((self.price * self.quantity * self.vat) * self.discount) - ((self.price * self.quantity) * self.discount)
super(InvoicesItems, self).save(**kwargs)
我不知道如何将计算的数据保存在 InvoiceItems
重新定义的保存函数中...这显然不起作用,因为 Invoices
得到首先保存...
views.py
def edit(request, id = None):
InvoiceFormSet = inlineformset_factory(Invoices, InvoicesItems)
if id == None:
initial_data = ''
data = Invoices()
else:
data = get_object_or_404(Invoices, pk=id)
initial_data = ''
if request.method == 'POST':
created_invoice = InvoicesForm(request.POST, instance=data)
form = InvoiceFormSet(request.POST, instance=data)
if not form.is_valid() and not created_invoice.is_valid():
//json err msg
else:
created_invoice.save()
form.save()
json = simplejson.dumps(response, ensure_ascii=False)
return HttpResponse(json, mimetype="application/json")
else:
form = InvoicesForm(instance=data, initial=initial_data)
form_items = InvoiceFormSet(instance=data)
c = {'form':form, 'form_items':form_items}
c.update(csrf(request))
return render_to_response('crud_invoice_edit.html', c)
如何迭代InvoiceItems
并计算然后需要插入到Invoices
中的字段。我是 django 新手...
谢谢!
This is my models.py
class Invoices(models.Model):
...
sum_w_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_wo_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_discount = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_vat = models.DecimalField(max_digits=7, decimal_places=2, default=0)
sum_paid = models.DecimalField(max_digits=7, decimal_places=2, default=0)
...
class InvoiceItems(models.Model):
invoice = models.ForeignKey(Invoices)
quantity = models.DecimalField(max_digits=9, decimal_places=2)
unit = models.ForeignKey(StocklistUnits, verbose_name='Merska enota')
price = models.DecimalField(max_digits=9, decimal_places=2)
vat = models.DecimalField(max_digits=4, decimal_places=3)
discount = models.DecimalField(max_digits=3, decimal_places=1)
def save(self, **kwargs):
self.invoice.sum_w_vat += (self.price * self.quantity * self.vat) * self.discount
self.invoice.sum_wo_vat += (self.price * self.quantity) * self.discount
self.invoice.sum_discount += (self.price * self.quantity) * ( self.discount / 100 )
self.invoice.sum_vat += ((self.price * self.quantity * self.vat) * self.discount) - ((self.price * self.quantity) * self.discount)
super(InvoicesItems, self).save(**kwargs)
I don't know how to save the calculated data in the InvoiceItems
redefined save function... this obviously doesn't work, because Invoices
get saved first...
views.py
def edit(request, id = None):
InvoiceFormSet = inlineformset_factory(Invoices, InvoicesItems)
if id == None:
initial_data = ''
data = Invoices()
else:
data = get_object_or_404(Invoices, pk=id)
initial_data = ''
if request.method == 'POST':
created_invoice = InvoicesForm(request.POST, instance=data)
form = InvoiceFormSet(request.POST, instance=data)
if not form.is_valid() and not created_invoice.is_valid():
//json err msg
else:
created_invoice.save()
form.save()
json = simplejson.dumps(response, ensure_ascii=False)
return HttpResponse(json, mimetype="application/json")
else:
form = InvoicesForm(instance=data, initial=initial_data)
form_items = InvoiceFormSet(instance=data)
c = {'form':form, 'form_items':form_items}
c.update(csrf(request))
return render_to_response('crud_invoice_edit.html', c)
How can I iterate through the InvoiceItems
and calculate the field which then need to be inserted into Invoices
. I'm new to django...
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否是正确的方法...但它有效...
我所要做的就是再次保存
created_invoice
...所以I don't know if this is the right way... but it works...
All I had to do was to save the
created_invoice
again... so