Reputation: 11481
I have the following code in a Django 2 project
print(List.objects.filter(user=self.request.user).query)
which prints the SQL query which is constructed
but the following
print(List.objects.filter(user=self.request.user).count().query)
throws error
print(List.objects.filter(user=self.request.user).count().query)
AttributeError: 'int' object has no attribute 'query'
I know why it happens, because count()
immediately return the count.
How do I see the query which it constructed
Upvotes: 1
Views: 359
Reputation: 2130
List.objects.filter(user=self.request.user).count()
This line returns count as type int
and integer type has no attribute query, but you can see the sql query behind this in your django shell
from django.db import connection
List.objects.filter(user=self.request.user).count()
connection.queries[-1]['sql']
connection.queries
returns list of dictionaries in that shell session.
Also don't name your model List
. It is a reserved keyword.
Upvotes: 5