How do I know if QuerySet Django has been rated?

I create a set of Django queries manually and just want to use Django ORM to read the resulting SQL Queryet.query without getting into my DB.

I know that Django quersets are lazy, and I see all the operating systems that trigger the query calculation:

https://docs.djangoproject.com/en/1.10/ref/models/querysets/#when-querysets-are-evaluated

But ... what if I just want to check my code, it simply builds the guts of the queries, but does NOT evaluate and does not hit my database unintentionally? Are there any attributes of the request object that I can use to verify that it was not rated without an actual rating?

+5
source share
2 answers

For queries that use select to return lists of model instances, such as a base filter or an exception, the _result_cache attribute _result_cache None if the query has not been evaluated or the list of results, if it has, The usual non-public attribute clauses apply.

For other queries (counting, deleting, etc.) I am not sure if there is an easy way. Perhaps browse the database logs or run in DEBUG mode and check the connection.queries as described here: https://docs.djangoproject.com/en/dev/faq/models/#how-can-i-see-the- raw-sql-queries-django-is-running

+5
source

For those who want to know when QuerySet are evaluated: when they iterate , chopped , pickled, or cached , repr () 'd, len () ' d, list () 'ed or bool () ' ed. See https://docs.djangoproject.com/en/1.10/ref/models/querysets/#when-querysets-are-evaluated

+1
source

Source: https://habr.com/ru/post/1259786/


All Articles