aggregate(Sum with calculated field

Hello Django guys :wink:

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 :wink:

Thanks helping me

Thais

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 that becomes part of your queryset, or, you can perform that calculation in your code after the data has been retrieved.

Thanks Ken for your help.

I modified my views with this part, but it look like a dirty solution :frowning:
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) … ???

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)

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 …

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. In the case of your Model method, it would be something like:

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

Yesss
Fantastic, thanks a lot for your help :grinning: :grinning:

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