Reputation: 31
My math kind of sucks at the moment. I actually have simple calculation that needs to be done for my invoices application.
The models are:
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)
I need to have clean way to calculate the total price of the products + the tax to show on the InvoiceDetail template. What is the best way to calculate this?
Upvotes: 0
Views: 2479
Reputation: 9443
You can perform addition with price
and tax
then aggregate
the result as total.
>>> 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
Upvotes: 2