Rakmo
Rakmo

Reputation: 1982

In Django, does `queryset is None` evaluates the queryset?

Is Django queryset evaluated in below cases? If no, then why?

1.

if queryset is None:
    pass

2.

from django.db.models.query import QuerySet

if isinstance(queryset, QuerySet):
    pass

Is it because in both the cases python performs object reference comparison, which does not lead to a query?

Upvotes: 0

Views: 332

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

As documented, a queryset is evaluated when you try to access its values - by iterating over it (directly or indirectly), subscribing/slicing it, printing it (actually: calling repr() on it) or testing it's truth value.

is is the identity operator, it compares objects identifiers, so it doesn't evaluate the queryset.

isinstance checks the object's class (and the class mro), so it doesn't evaluate the queryset either.

Upvotes: 3

Related Questions