When I am using raw sql for a variable say myvar = some rawsql and when I am checking in if condition in query for myvar it is always true.
{% if myvar %}
do this;
{% else %}
do this;
{% endif %}
For instance my raw sql returns 0 records then I want to display some message, but I could not able to do that. When I debugged whats happening in the background, my raw sql always returns some object (<RawQuerySet: "sel) eventhough sql fetches empty records. So thats why myvar is always true because its holding some object of raw queryset.
Is there any way to getrid of this situation
Thanks in advance
You can avoid this situation by passing a list instead of a raw queryset:
Slicing would also work:
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:
Using a for loop if you are iterating works as well depending on what you are trying to do: