Django根据formset中的另外两个字段计算字段

时间:2014-10-19 15:32:54

标签: python django django-forms

我有两种形式OrderHeaderForm和OrderLineForm。 OrderHeaderForm显示客户和总价。 OrderLineForm是一个表单,显示产品,数量,价格和总数。

我正在尝试计算OrderLineForm中的行总数(数量*价格)。最好在调整数量或价格时重新计算总数,但我在如何创建和显示此计算时会丢失。任何关于如何实现这样的事情的方向提示或提示将不胜感激。

编辑:我让它工作,看下面编辑过的代码,但在我看来,这似乎是计算表单字段的“脏”方式。此时的计算仅在POST后出现。有关更清洁和/或实时计算方法的建议吗?

models.py中的新代码:

def linetotalprice(self):
    linetotalprice = self.orderline_product_price * self.orderline_quantity
    return linetotalprice

views.py中的新代码

if form.is_valid() and formset.is_valid():
    saveform = form.save(commit=False)
    saveform.save()
    formset.save()
    orderheader = OrderHeader.objects.get(pk=orderid.pk)
    orderlines = OrderLine.objects.filter(orderheader=orderid)
    orderheader.orderheader_total_price = 0
    for orderline in orderlines:  
        orderline.orderline_total_price = orderline.linetotalprice()
        orderheader.orderheader_total_price += orderline.linetotalprice()
        orderline.save()
        orderheader.save()

我的models.py

class OrderHeader(models.Model):
    customer = models.ForeignKey(Customer)
    orderheader_total_price = models.DecimalField()

    def __unicode__(self):
        return smart_unicode(self.pk)

class OrderLine(models.Model):
    orderheader = models.ForeignKey(OrderHeader)
    product = models.ForeignKey(Product)
    material = models.ForeignKey(ProductMaterial)
    orderline_quantity = models.DecimalField()
    orderline_product_price = models.DecimalField()
    orderline_total_price = models.DecimalField()

    def __unicode__(self):
        return smart_unicode(self.pk)

    def linetotalprice(self):
        linetotalprice = self.orderline_product_price * self.orderline_quantity
        return linetotalprice

my views.py

if request.method == 'POST':
    form = OrderHeaderForm(request.POST, instance=orderid)
    formset = OrderLineFormSet(request.POST,instance=orderid)
    if form.is_valid() and formset.is_valid():
        form.save()
        formset.save()
        orderheader = OrderHeader.objects.get(pk=orderid.pk)
        orderlines = OrderLine.objects.filter(orderheader=orderid)
        orderheader.orderheader_total_price = 0
        for orderline in orderlines:  
            orderline.orderline_total_price = orderline.linetotalprice()
            orderheader.orderheader_total_price += orderline.linetotalprice()
            orderline.save()
            orderheader.save()
        messages.success(request, 'Order saved')
    else:
        messages.error(request, 'Order save error, please check fields below')
else:
    form = OrderHeaderForm(instance=orderid)
    formset = OrderLineFormSet(instance=orderid)

1 个答案:

答案 0 :(得分:1)

使用Javascript实时计算并显示该字段值的总计。

在您看来,您不应该进行查询以同时创建OrderHeaderOrderLinesform.save()返回此实例

做:

OrderHeader = form.save()
OrderLines  = formset.save()

我更喜欢

for orderline in orderlines:  
            orderline.orderline_total_price = orderline.linetotalprice()
            orderheader.orderheader_total_price += orderline.orderline_total_price // avoid recalculating, use that value calculated just before
            orderline.save()
            //orderheader.save() avoid saving each time for this model
 orderheader.save() // saving one time :)

您也可以将linetotalprice函数更改为直接返回:

def linetotalprice(self):
    return self.orderline_product_price * self.orderline_quantity

并将 orderline_total_price 默认值设为0