Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Class-based Views Customizing Class-based Views Override get_queryset

Simon Amz
Simon Amz
4,606 Points

How to return zero records?

In the exercise, I have to return zero records if the term to look for is blank, but the bummer message is not clear enough I don't know where the error comes from.

Thanks for the clues.

articles/views.py
from django.views import generic

from . import models


class ArticleList(generic.ListView):
    model = models.Article


class ArticleDetail(generic.DetailView):
    model = models.Article


class ArticleCreate(generic.CreateView):
    fields = ('title', 'body', 'author', 'published')
    model = models.Article


class ArticleUpdate(generic.UpdateView):
    fields = ('title', 'body', 'author', 'published')
    model = models.Article


class ArticleDelete(generic.DeleteView):
    model = models.Article


class ArticleSearch(generic.ListView):
    model = models.Article

    def get_queryset(self):
        if self.kwargs["term"]:
            return self.model.objects.filter(model.body__icontains=self.kwargs["term"])
        else:
            return

1 Answer

Quentin Vadon
Quentin Vadon
2,263 Points

Hi,

To return an empty queryset you can do MyModel.objects.none() ;-)