Ravi Kumar
Ravi Kumar

Reputation: 1402

Django Queryset for no-operation

Is there any way to specify a Django Queryset, which will do nothing but still be valid Queryset? Empty queryset should ideally not call DB and results would be empty.

Upvotes: 2

Views: 268

Answers (2)

K Z
K Z

Reputation: 30453

Use MyModel.objects.none() which will return an EmptyQuerySet, which is a QuerySet subclass that always evaluates to an empty list.

For eg., if you have a model class Entry (or just use models.Model), you can simply create an EmptyQuerySet by:

>>> Entry.objects.none()
[]

Upvotes: 1

mossplix
mossplix

Reputation: 3865

Model.objects.none()
always gives you an empty queryset

Upvotes: 4

Related Questions