>>> Author.objects.raw("""select * from stack_author where id = 5""")
<RawQuerySet: 'select * from stack_author where id = 5'>
>>> list(_)
[]
>>>
>>> if Author.objects.raw("""select * from stack_author where id = 5"""):
... print 'yes'
...
yes
You can avoid this situation by passing a list instead of a raw queryset:
>>> if list(Author.objects.raw("""select * from stack_author where id = 5""")):
... print 'yes'
...
>>>
Slicing would also work:
>>> if Author.objects.raw("""select * from stack_author where id = 5""")[:]:
... print 'yes'
...
>>>
You could also evaluate the qs in the view and pass a boolean result to the template.
Careful that Indexing will raise an IndexError on an empty raw qs:
>>> if Author.objects.raw("""select * from stack_author where id = 5""")[0]:
... print 'yes'
...
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/dennisting/.virtualenvs/django-sb/lib/python2.7/site-packages/django/db/models/query.py", line 1379, in __getitem__
return list(self)[k]
IndexError: list index out of range
Using a for loop if you are iterating works as well depending on what you are trying to do:
>>> for author in Author.objects.raw("""select * from stack_author where id = 5"""):
... print author
...
>>>