use Count() to get the number of paid orders for the product

This commit is contained in:
Thomas Steen Rasmussen 2021-07-22 08:02:28 +02:00
parent b76cc2ed64
commit f61d0b85e8
2 changed files with 3 additions and 2 deletions

View file

@ -31,7 +31,7 @@
<td class="text-right">{{ p.price|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ p.cost|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ p.profit|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-center">{{ p.order_set.count }}</td>
<td class="text-center">{{ p.order_count }}</td>
<td class="text-center">{{ p.total_units_sold }}</td>
<td class="text-right">{{ p.total_revenue|floatformat:"2" }}&nbsp;DKK</td>
<td class="text-right">{{ p.total_cost|floatformat:"2" }}&nbsp;DKK</td>

View file

@ -7,7 +7,7 @@ from django.contrib import messages
from django.contrib.postgres.fields import DateTimeRangeField
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import F, Sum
from django.db.models import Count, F, Sum
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.dateparse import parse_datetime
@ -403,6 +403,7 @@ class ProductStatsManager(models.Manager):
.annotate(total_revenue=F("price") * F("total_units_sold"))
.annotate(total_cost=F("cost") * F("total_units_sold"))
.annotate(total_profit=F("profit") * F("total_units_sold"))
.annotate(order_count=Count("orderproductrelation__order"))
)