4

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 ?

5
  • 1
    Notice that your ORM query is asigned to filterd and you are printing filterf. Extra d at the end of the variable name
    – e4c5
    Commented Jun 3, 2016 at 14:44
  • @e4c5 thanx , i cant believe it was a typo !
    – hretic
    Commented Jun 3, 2016 at 14:52
  • @hretic keep in mind that you will get a .query string version from .filter() but you'll get an error if you perform an .update() or .get() (etc.). You could also enable logs in your DB engine and see the incoming queries in real time.
    – Gocht
    Commented Jun 3, 2016 at 14:54
  • @Gocht so what is solution for update ? @ betonimig way ? or should i just do a filter/select to see the query and after that do a update ?
    – hretic
    Commented Jun 3, 2016 at 14:57
  • I had a similar need using PostgreSQL and to get the query executed in a .update() I enabled logs. See stackoverflow.com/a/722236/3945375 Then you can see incoming queries tracing the log file. You can look for the similar steps for your DB engine.
    – Gocht
    Commented Jun 3, 2016 at 15:03

1 Answer 1

6

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.