简体   繁体   中英

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?

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.

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