Bladeren bron

feat :star: reduce db calls by counting offers in voucher listing

master
Samar Hassan 2 jaren geleden
bovenliggende
commit
3daf3dda59

+ 3
- 1
src/oscar/apps/dashboard/vouchers/views.py Bestand weergeven

@@ -3,6 +3,7 @@ import csv
3 3
 from django.conf import settings
4 4
 from django.contrib import messages
5 5
 from django.db import transaction
6
+from django.db.models import Count
6 7
 from django.http import HttpResponse
7 8
 from django.shortcuts import get_object_or_404, redirect
8 9
 from django.urls import reverse, reverse_lazy
@@ -33,8 +34,9 @@ class VoucherListView(generic.ListView):
33 34
     def get_queryset(self):
34 35
         self.search_filters = []
35 36
         qs = self.model._default_manager.all()
37
+        qs = qs.annotate(num_offers=Count('offers'))
36 38
         qs = sort_queryset(qs, self.request,
37
-                           ['num_basket_additions', 'num_orders',
39
+                           ['num_basket_additions', 'num_orders', 'num_offers',
38 40
                             'date_created'],
39 41
                            '-date_created')
40 42
 

+ 2
- 2
src/oscar/templates/oscar/dashboard/vouchers/voucher_list.html Bestand weergeven

@@ -90,7 +90,7 @@
90 90
                         <th>{% trans "Name" %}</th>
91 91
                         <th>{% trans "Code" %}</th>
92 92
                         <th>{% trans "Status" %}</th>
93
-                        <th>{% trans "Num offers" %}</th>
93
+                        <th>{% anchor 'num_offers' _("Num Offers") %}</th>
94 94
                         <th>{% anchor 'num_basket_additions' _("Num baskets") %}</th>
95 95
                         <th>{% anchor 'num_orders' _("Num orders") %}</th>
96 96
                         <th>{% anchor 'date_created' _("Date created") %}</th>
@@ -114,7 +114,7 @@
114 114
                                     <span class="badge badge-secondary">{% trans "Inactive" %}</span>
115 115
                                 {% endif %}
116 116
                             </td>
117
-                            <td>{{ voucher.offers.count }}</td>
117
+                            <td>{{ voucher.num_offers }}</td>
118 118
                             <td>{{ voucher.num_basket_additions }}</td>
119 119
                             <td>{{ voucher.num_orders }}</td>
120 120
                             <td>{{ voucher.date_created }}</td>

Laden…
Annuleren
Opslaan