Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ORM : how can i see last executed query on fly

Tags:

python

orm

django

so lets say we have a simple query using django orm

filterd = User.objects.exclude(id = request.user.id ).filter(username=data['username'] )

this is suppose to return some objects but it returns none ! obviously i'm doing something wrong as i' not comfortable with django ORM yet , so i'll help alot to know what query is exactly executed in this line

i've searched around i found this

print(filter.query)

but i get

AttributeError: type object 'filter' has no attribute 'query'

i guess filter is None when no object is returned so ... what should i do ?

like image 925
hretic Avatar asked Oct 24 '25 14:10

hretic


1 Answers

Try:

from django.db import connection as conn
filterd = User.objects.exclude(id = request.user.id).filter(username=data['username'] )
# to execute query
print filterd
print conn.queries
like image 87
ILdar Migranov Avatar answered Oct 27 '25 02:10

ILdar Migranov