32👍
For querysets that use a select
to return lists of model instances, like a basic filter or exclude, the _result_cache
attribute is None
if the queryset has not been evaluated, or a list of results if it has. The usual caveats about non-public attributes apply.
Note that printing a queryset – although the docs note calling repr()
as an evaluation trigger – does not in fact evaluate the original queryset. Instead, internally the original queryset chains into a new one so that it can limit the amount of data printed without changing the limits of the original queryset. It’s true that it evaluates a subset of the original queryset and therefore hits the DB, so that’s another weakness of this approach if you’re in fact trying to use it to monitor all DB traffic.
For other querysets (count, delete, etc) I’m not sure there is a simple way. Maybe watch your database logs, or run in DEBUG
mode and check connection.queries
as described here:
https://docs.djangoproject.com/en/dev/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running