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

  • SEARCH
  • Home
  • 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 9160959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:51:36+00:00 2026-06-17T13:51:36+00:00

I see that the QuerySet class has 2 different methods that seem to serve

  • 0

I see that the QuerySet class has 2 different methods that seem to serve the same purpose (unless I’m mistaken): .__nonzero__ and .exists. (Yes, I know that .__nonzero__ is used by bool.)

My question: Why do the 2 methods have different implementation if they both just check whether there’s any object in the queryset?

The Django documentation says about QuerySet.__nonzero__:

Note: Don’t use this if all you want to do is determine if at least
one result exists, and don’t need the actual objects. It’s more
efficient to use exists() (see below).

(I didn’t find anything insightful “below”.)

Why does QuerySet.__nonzero__ have a non-efficient implementation? Is it trying to achieve something different than .exists? What is the reason that the Django developers don’t do __nonzero__ = exists?

  • 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-06-17T13:51:37+00:00Added an answer on June 17, 2026 at 1:51 pm

    Why does QuerySet.nonzero have a non-efficient implementation? Is it trying to achieve something different than .exists? What is the reason that the Django developers don’t do nonzero = exists?

    I think it’s because exists is only efficient under certain circumstances.

    Imagine this common scenario if __nonzero__ had the “efficient” implementation.

    foos = Foo.objects.filter(bar='baz')
    
    if foos:   # nonzero() calls exists() which causes extra query
               # even though the QS is already going to be evaluated
               # which in my projects is a common pattern. 
       print "Yay for foos!"
       for foo in foos:
           print foo
    

    __nonzero__ also evaluates the query and stores results in cache. That means all results are stored in memory.

    exists only cares about 1 row, and doesn’t even instantiate a django object or even store its result in cache.

    It seems useful if you are only checking if something exists, and don’t need the data in any way.

    Why don’t the devs make __nonzero__ == exists?

    Because exists assumes you don’t care about the results. If __nonzero__ called exists, we’d have no results. If exists called __nonzero__, we’d collect results (potentially a lot) just to check if a row exists or not.

    Examples:

    bool( Foo.objects.filter(user=user) )  
    # calls __nonzero__, evaluates, converts all fields to python objects 
    # and stores in queryset._result_cache
    
    
    Foo.objects.filter(user=user).exists()
    # stores nothing, and query only returns one row.
    # less data from DB and less python overhead generating django model instances.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see that this is the same question as Making cmake print commands before
I see that we cannot use if not exists in the create virtual table
I see that Spring has a @Required annotation to mark member variables in beans
I see that in different plugins and codes, but I don't understand what does
In my Tastypie resource, I'm annotating my queryset, and yet I don't see that
I see that most of big websites are returning javascript objects instead of pure
I see that many people get this error, but their situations all appear a
I see that QButtonGroup s let you throw in an integer when you do
I see that there is PostSharp AOP support for Silverlight, but is there a
I see that tax information is kept at order level but I cannot see

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.