Django用税计算总价

时间:2018-05-29 05:44:03

标签: python django calculator

此刻我的数学很糟糕。实际上,我的发票应用程序需要进行简单的计算。

模型是:

class Product(models.Model):
    name = models.Charfield(max_lenght=50)
    price = models.DecimalField(decimal_places=2, max_digits=10)
    tax = models.PositiveIntegerField()

Class Invoice(models.Model):
    client = models.ForeignKey(Client)
    date = models.DateField()


class Specification(models.Model):
    invoice = models.ForeignKey(Invoice)
    product = models.ForeignKey(Invoice)
    timestamp = models.DateTimeField(auto_now_add=True)

我需要有干净的方法来计算产品的总价+在InvoiceDetail模板上显示的税。计算这个的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以使用pricetax然后aggregate将结果作为总计执行添加。

>>> from django.db.models import F, Sum
>>> Product.objects
      .filter(your_filter_here)
      .aggregate(total=Sum(F('price') + F('tax')), output_field=FloatField())

Output
total: 12345.67
相关问题