[Answered ]-Django 1.8 – 'NoneType' object has no attribute 'aggregate'

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

Leave a comment