Skip to content

Commit 8bdec3c

Browse files
5 - Get Data by Custom QuerySet
1 parent 0438bd0 commit 8bdec3c

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/analytics/templates/analytics/sales.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h1>Sales Data</h1>
1212

1313
<div class='row'>
1414
<div class='col'>
15-
<p>Recent Total: ${{ recent_orders_total.total__sum }}</p>
15+
<p>Recent Total: ${{ recent_orders_data.total__sum }}</p>
1616
<ol>
1717
{% for order in recent_orders %}
1818
<li>{{ order.order_id }}
@@ -22,7 +22,7 @@ <h1>Sales Data</h1>
2222
</ol>
2323
</div>
2424
<div class='col'>
25-
<p>Shipped</p>
25+
<p>Shipped Total: ${{ shipped_orders_data.total__sum }}</p>
2626
<ol>
2727
{% for order in shipped_orders %}
2828
<li>{{ order.order_id }}
@@ -32,7 +32,7 @@ <h1>Sales Data</h1>
3232
</ol>
3333
</div>
3434
<div class='col'>
35-
<p>Paid</p>
35+
<p>Paid Totals: ${{ paid_orders_data.total__sum }}</p>
3636
<ol>
3737
{% for order in paid_orders %}
3838
<li>{{ order.order_id }}

src/analytics/views.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,11 @@ def get_context_data(self, *args, **kwargs):
2323
qs = Order.objects.all()
2424
context['orders'] = qs
2525
context['recent_orders'] = qs.recent().not_refunded()[:5]
26-
context['recent_orders_total'] = context['recent_orders'].aggregate(
27-
Sum("total"),
28-
Avg("total"),
29-
# Avg("cart__products__price"),
30-
# Count("cart__products")
31-
)
32-
# context['recent_cart_data'] = context['recent_orders'].aggregate(
33-
# Avg("cart__products__price"),
34-
# Count("cart__products")
35-
# )
36-
# qs = Order.objects.all().aggregate(Sum("total"), Avg("total"), Avg("cart__products__price"), Count("cart__products"))
37-
# ann = qs.annotate(product_avg=Avg('cart__products__price'), product_total = Sum('cart__products__price'), product__count = Count('cart__products'))
38-
context['shipped_orders'] = qs.recent().not_refunded().by_status(status='shipped')[:5]
39-
context['paid_orders'] = qs.recent().not_refunded().by_status(status='paid')[:5]
26+
context['recent_orders_data'] = context['recent_orders'].totals_data()
27+
context['recent_orders_cart_data'] = context['recent_orders'].cart_data()
28+
context['shipped_orders'] = qs.recent().not_refunded().by_status(status='shipped')[:1]
29+
context['shipped_orders_data'] = context['shipped_orders'].totals_data()
30+
context['paid_orders'] = qs.recent().not_refunded().by_status(status='paid')[:4]
31+
context['paid_orders_data'] = context['paid_orders'].totals_data()
32+
4033
return context

src/orders/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import math
22
from django.conf import settings
33
from django.db import models
4+
from django.db.models import Count, Sum, Avg
45
from django.db.models.signals import pre_save, post_save
56
from django.core.urlresolvers import reverse
67

@@ -22,6 +23,16 @@ class OrderManagerQuerySet(models.query.QuerySet):
2223
def recent(self):
2324
return self.order_by("-updated", "-timestamp")
2425

26+
def totals_data(self):
27+
return self.aggregate(Sum("total"), Avg("total"))
28+
29+
def cart_data(self):
30+
return self.aggregate(
31+
Sum("cart__products__price"),
32+
Avg("cart__products__price"),
33+
Count("cart__products")
34+
)
35+
2536
def by_status(self, status="shipped"):
2637
return self.filter(status=status)
2738

0 commit comments

Comments
 (0)