简体   繁体   中英

django 1.8 - 'NoneType' object has no attribute 'aggregate'

The error is self clear. But I was puzzled at how to solve it.

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.

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.

Any suggestion is appreciated, thanks in advance.

views.py

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'))

EDIT as suggest

.........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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM