From 564d95f701024efeacc3e3d7b63bbc3d61fa2c50 Mon Sep 17 00:00:00 2001 From: imperosol Date: Sun, 13 Oct 2024 10:37:48 +0200 Subject: [PATCH] fix: InvoiceQuerySet.annotate_total() (but for real this time) --- core/tests/test_user.py | 3 ++- eboutic/models.py | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/tests/test_user.py b/core/tests/test_user.py index c767f3f4..0fdf43c9 100644 --- a/core/tests/test_user.py +++ b/core/tests/test_user.py @@ -158,10 +158,11 @@ def test_user_invoice_with_multiple_items(): item_recipe = Recipe(InvoiceItem, invoice=foreign_key(Recipe(Invoice, user=user))) item_recipe.make(_quantity=3, quantity=1, product_unit_price=5) item_recipe.make(_quantity=1, quantity=1, product_unit_price=5) + item_recipe.make(_quantity=2, quantity=1, product_unit_price=iter([5, 8])) res = list( Invoice.objects.filter(user=user) .annotate_total() .order_by("-total") .values_list("total", flat=True) ) - assert res == [15, 5] + assert res == [15, 13, 5] diff --git a/eboutic/models.py b/eboutic/models.py index 468562ad..7ec9deef 100644 --- a/eboutic/models.py +++ b/eboutic/models.py @@ -173,9 +173,8 @@ class InvoiceQueryset(models.QuerySet): return self.annotate( total=Subquery( InvoiceItem.objects.filter(invoice_id=OuterRef("pk")) - .annotate(item_amount=F("product_unit_price") * F("quantity")) - .values("item_amount") - .annotate(total=Sum("item_amount")) + .values("invoice_id") + .annotate(total=Sum(F("product_unit_price") * F("quantity"))) .values("total") ) )