title: django 1.8 - 'NoneType' object has no attribute 'aggregate'
body: <p>The error is self clear. But I was puzzled at how to solve it.</p>
  
  <p>If the form entry get the corresponding record in the database, then there is no error. But if I enter something in the form that doesn't have corresponding data in the database, then it would appear the error of 'NoneType' object has no attribute 'aggregate'. I know the reason is because no query result, so in def context it cannot continue the calculation.</p>
  
  <p>So if there is anyway if not finding corresponding record in the database, then it will not continue to do the calculation in "def get_context_data" to avoid this error? So the page will just remain on the current data entry page without redirecting to the another page to show the result.</p>
  
  <p>Any suggestion is appreciated, thanks in advance.</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>from django.contrib import messages
  
  class FinalView(ListView):
      context_object_name = 'XXX'
      template_name = 'XXX.html'
      model = Final
  
      def get_queryset(self):
          form = InputForm(self.request.GET)        
          if form.is_valid():
              department = form.cleaned_data['department']
              person = form.cleaned_data['person']
  
              if department !="" and person !="":
                  if Final.objects.filter(department=department,person=person).exists():
                      queryset=Final.objects.filter(department=department,person=person)
                      return queryset
                  else:
                      form.add_error(ValidationError('No corresponding data exists'))
  
              elif department =="" and person !="":
                  if Final.objects.filter(person=person).exists():
                      queryset=Final.objects.filter(person=person)
                      return queryset
                  else:
                      form.add_error(ValidationError('No corresponding data exists'))
  
              elif ........
  
  
          else:     #if form not valid
              messages.error(request, "Error")
  
      def get_context_data(self,**kwargs):
          context["sales"] = self.get_queryset().aggregate(Sum('sales'))
  </code></pre>
  
  <p><strong>EDIT as suggest</strong></p>
  
  <pre><code>.........Before are the same....
  
   def get_context_data(self,**kwargs):
          query_set = self.get_queryset()
          if query_set is not None:
              context = super(FinalView, self).get_context_data(**kwargs)
              context["sales"] = query_set.aggregate(Sum('sales'))
              return context
  </code></pre>