Django inlineformset - 自定义保存方法

发布于 2024-11-09 13:38:24 字数 2527 浏览 0 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-11-16 13:38:24

我不知道这是否是正确的方法...但它有效...

我所要做的就是再次保存created_invoice...所以

created_invoice.save()
form.save()
created_invoice.save()

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

created_invoice.save()
form.save()
created_invoice.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文