Erik
Erik

Reputation: 7731

What is the correct way of returning an empty queryset in a Django View?

I have a ListView in Django whose get_queryset() method will sometimes need to return no results. I've tried three ways to do this:

  1. return EmptyQuerySet()
  2. return Model.objects.none()
  3. return Model.objects.filter(pk=-1)

Each one of these returns a slightly different object.

  1. django.db.models.query.EmptyQuerySet with its model attribute set to None
  2. django.db.models.query.EmptyQuerySet with its model attribute set to Model
  3. django.db.models.query.QuerySet with its model attribute set to Model

Only the third option works with the class based ListView. The other options crash on an attribute error when the ListView tries to access the model attribute. This surprises me and is a pain as it requires me to import Model in places where that can cause MRO issues.

What am I doing wrong/what should I be doing differently?

Update: The question then is, what is the right way to return an empty queryset via the class view method get_queryset()?

Update: Here is the line in Django's generic views that hits an attribute error when trying access the model attribute: https://github.com/django/django/blob/stable/1.5.x/django/views/generic/list.py#L166.

Upvotes: 43

Views: 34064

Answers (1)

Harel
Harel

Reputation: 1337

I think the best way to accomplish this is to call none() on objects for your respective model, and return the result. Assuming your model is named Entry:

queryset = Entry.objects.none()

Upvotes: 94

Related Questions