2👍
✅
Instead of trying to force context into get_queryset
, you can pull the object_list
into get_context_data
:
def get_queryset(self):
filter_month = self.request.GET.get('filtromes', '0')
if filter_month == "0":
return Order.objects.filter(status = 'recibido_pagado')
else:
return (Order.objects
.filter(created__month=filter_month, status = 'recibido_pagado'))
def get_context_data(self, **kwargs):
context = super(OrdersListView, self).get_context_data(**kwargs)
qs = kwargs.pop('object_list', self.object_list)
order = self.request.GET.get('orderby', 'created')
context = {}
revenue = 0
for order in qs:
revenue += order.total - order.shipping_cost
context['revenue'] = revenue
context['filtromes'] = self.request.GET.get('filtromes', '0')
context['orderby'] = self.request.GET.get('orderby', 'created')
context['category'] = "catalogo"
return context
Also consider using annotate
, Sum
, and/or aggregate
to calculate the revenue
Source:stackexchange.com