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