Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5930695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:35:22+00:00 2026-05-22T14:35:22+00:00

When I am using raw sql for a variable say myvar = some rawsql

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:35:23+00:00Added an answer on May 22, 2026 at 2:35 pm
    >>> 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
    ... 
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a counter function which updates a counter using raw SQL:
I want to execute raw SQL using Doctrine 2 I need to truncate the
I have a raw sql query I need to run, but the database name
I know about prepared statements, but if I'm using raw SQL, does ActiveRecord have
I'm writing a bulk insert script using Django's ORM + custom raw SQL. The
I'm trying to insert rows using raw sql in django: The input file has
I'm using the following custom handler for doing bulk insert using raw sql in
We're using Doctrine, a PHP ORM. I am creating a query like this: $q
How can I execute direct/raw SQL through subsonic 3.0 when using the Active Record
I've got a Doctrine_RawSql query using prepared statements. However, they seem to get ignored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.