# aggregate(Sum with calculated field

**URL:** <https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801>\
**Category:** Using the ORM\
**Created:** [July 16, 2021, 2:26pm UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801 "2021-07-16T14:26:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thais781](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thais781/32/4775_2.png) [@thais781](https://forum.djangoproject.com/u/thais781)\
**Post date:** [July 16, 2021, 2:26pm UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801/1 "2021-07-16T14:26:11Z")

</div>

Hello Django guys 😉

I have a small question that I cannot solve.  
I am trying to make a Sum on a calculated fields

**My Models**  
class Finances\_Lignes(models.Model):  
quantite = models.DecimalField(max\_digits=8, decimal\_places=2, null=True)  
prix\_unitaire = models.DecimalField(max\_digits=8, decimal\_places=2, null=True)

def prix\_total(self):  
my\_prix\_total = self.quantite \* self.prix\_unitaire  
my\_prix\_total = round(my\_prix\_total,2)  
return my\_prix\_total

**My Views**  
my\_finances\_lignes = Finances\_Lignes.objects.all()  
total\_price = my\_finances\_lignes.aggregate(Sum(‘prix\_total’))

**And the error is :**  
Cannot resolve keyword ‘prix\_total’ into field.

If you have an idea do not hesitate 😉

Thanks helping me

Thais

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 16, 2021, 2:30pm UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801/2 "2021-07-16T14:30:59Z")

</div>

That’s correct. You cannot “integrate” code written in Python that is running in your web server with the query code that runs in the database.

To do what you’re intending, you’re either going to need to convert that function to a [query expression](https://docs.djangoproject.com/en/3.2/ref/models/expressions/#query-expressions) that becomes part of your queryset, or, you can perform that calculation in your code after the data has been retrieved.

---

<div class="post-metadata">

**Author:** ![thais781](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thais781/32/4775_2.png) [@thais781](https://forum.djangoproject.com/u/thais781)\
**Post date:** [July 16, 2021, 5:13pm UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801/3 "2021-07-16T17:13:47Z")

</div>

Thanks Ken for your help.

I modified my views with this part, but it look like a dirty solution ☹  
**My Views**  
total\_price = 0  
for my\_ligne in my\_finances\_lignes: total\_price = total\_price + my\_ligne.prix\_total()

I will search for another way : total price for a parent object = the sum of the price of all the childs (and define it in the model to access it easly) … ???

---

<div class="post-metadata">

**Author:** ![thais781](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thais781/32/4775_2.png) [@thais781](https://forum.djangoproject.com/u/thais781)\
**Post date:** [July 16, 2021, 5:19pm UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801/4 "2021-07-16T17:19:20Z")

</div>

For exemple if I am not clear (that’s it really possible):

Models.py  
class Finances(models.Model):  
ref = models.CharField(max\_length = 50, blank=True, null=True)

```auto
def total_price(self):
      ****loop on all my childs*****
            my_temp = self.CHILD.quantite * self.CHILD.prix_unitaire
            my_prix_total = my_prix_total + round(my_temp,2)
      return my_prix_total

```

class Finances\_Lignes(models.Model):  
quantite = models.DecimalField(max\_digits=8, decimal\_places=2, null=True)  
prix\_unitaire = models.DecimalField(max\_digits=8, decimal\_places=2, null=True)  
finances = models.ForeignKey(Finances, on\_delete=models.SET\_NULL, null=True)

But I don’t know if the code part is possible or how to do it …

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 16, 2021, 7:46pm UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801/5 "2021-07-16T19:46:34Z")

</div>

First, side note - when posting code here, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This ensures your code stays properly formatted and special character sequences don’t get interpreted. (If you are inclined to do so, I’d encourage you to edit your original post and insert those lines of ``` before and after your code.)

I think the question you’re trying to ask is, "How do I retrieve all `Finances_Lignes` objects that refer to a `Finances` instance?

If that’s the case, you can access that set of Finances\_Lignes using the [Related Manager](https://docs.djangoproject.com/en/3.2/ref/models/relations/#related-objects-reference). In the case of your Model method, it would be something like:

```auto
for a_finances_lignes in self.finances_lignes_set.all():
    # Do something with that instance of the Finances_Lignes
...

```

---

<div class="post-metadata">

**Author:** ![thais781](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/thais781/32/4775_2.png) [@thais781](https://forum.djangoproject.com/u/thais781)\
**Post date:** [July 19, 2021, 8:20am UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801/6 "2021-07-19T08:20:14Z")

</div>

Yesss  
Fantastic, thanks a lot for your help 😀 😀

---

<div class="post-metadata">

**Author:** ![Hugo-mesk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hugo-mesk/32/12495_2.png) [@Hugo-mesk](https://forum.djangoproject.com/u/Hugo-mesk)\
**Post date:** [March 3, 2023, 1:02pm UTC](https://forum.djangoproject.com/t/aggregate-sum-with-calculated-field/8801/7 "2023-03-03T13:02:51Z")

</div>

Maybe you can do it at the database level:  
from django.db.models import F

total\_price = my\_finances\_lignes.aggregate(prix\_total=Sum(F(‘amount’) \* F(‘prix\_unitaire’))  
total\_price = round(total\_price[‘total\_price’], 2)

I haven’t tested yet
