Mam trzy modele uproszczone dla przykładu:Opisywanie sumą dwóch pól pomnożone
class Customer(models.Model):
email = models.CharField(max_length=128)
class Order(models.Model):
customer = models.ForeignKey(Customer)
order_status = models.CharField(blank=True, max_length=256)
class Lineitem(models.Model):
order = models.ForeignKey(Order)
quantity = models.IntegerField(blank=True)
price = models.DecimalField(max_digits=6, decimal_places=2)
Chcę zapytać klientów (ewentualnie z filtrem) i opisywanie łącznie wydali (czyli suma ciągu (cena * ilość)
próbowałem.?
Customer.objects.filter(something).annotate(total_spent=Sum(F('order__lineitem__quantity') * F('order__lineitem__price')))
Wydaje się, że Sum() nie może być używany z F() wyrażeń Czy jest jakiś inny sposób to zrobić
Czy kiedykolwiek uzyskać to do pracy? – dotty
Szukam również rozwiązania – Don
Możliwy duplikat [Django Aggregation: Podsumowanie mnożenia dwóch pól] (http://stackoverflow.com/questions/12165636/django-aggregation-summation-of-multiplication-of-two -fields) – Louis