2π
β
Use the fact that django query sets are are lazy to your advantage, it wonβt make a database query till it needs the data so just split it up to do some validation
query_set = self.get_queryset()
context = super(FinalView, self).get_context_data(**kwargs)
if query_set is not None:
context['sales'] = query_set.aggregate(Sum('sales'))
return context
You may also want to make sure that all paths from get_queryset
do actually return something to make this kind of validation easier.
π€Sayse
Source:stackexchange.com