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 8270877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:42:30+00:00 2026-06-08T06:42:30+00:00

Recently found something peculiar in a filter, I can’t believe its intended behaviour. from

  • 0

Recently found something peculiar in a filter, I can’t believe its intended behaviour.

from django.contrib.auth.models import User
print User.objects.filter(id__in=User.objects.none().values_list("id",flat=True))
print User.objects.filter(id__in=User.objects.all().values_list("id",flat=True))

Oddly both of these lists return the full set of users. It actually seems to be pretty easy to “fix” if I wrap the inner query in a list function e.g.

User.objects.filter(id__in=list(User.objects.none().values_list("id")))

Then this returns what I would expect (an empty list).

Seems like a bug to me, or am I missing something?

Steve

  • 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-08T06:42:31+00:00Added an answer on June 8, 2026 at 6:42 am

    Here’s the queries produced for both:

    User.objects.filter(id__in=User.objects.none().values_list(“id”,flat=True))

    SELECT "auth_user"."id",
           "auth_user"."username",
           "auth_user"."first_name",
           "auth_user"."last_name",
           "auth_user"."email",
           "auth_user"."password",
           "auth_user"."is_staff",
           "auth_user"."is_active",
           "auth_user"."is_superuser",
           "auth_user"."last_login",
           "auth_user"."date_joined"
    FROM "auth_user"
    WHERE "auth_user"."id" IN
        (SELECT U0."id"
         FROM "auth_user" U0) LIMIT 21
    

    User.objects.filter(id__in=User.objects.all().values_list(“id”,flat=True))

    SELECT "auth_user"."id",
           "auth_user"."username",
           "auth_user"."first_name",
           "auth_user"."last_name",
           "auth_user"."email",
           "auth_user"."password",
           "auth_user"."is_staff",
           "auth_user"."is_active",
           "auth_user"."is_superuser",
           "auth_user"."last_login",
           "auth_user"."date_joined"
    FROM "auth_user"
    WHERE "auth_user"."id" IN
        (SELECT U0."id"
         FROM "auth_user" U0) LIMIT 21
    

    Notice anything? They’re exactly the same queries. Also interesting is what happens if you try things like User.objects.none(), User.objects.filter(id__in=[]) and User.objects.filter(id__in=User.objects.none(). In all three of these circumstances, Django short-circuits the query. In other words, it doesn’t even issue a query to the database because it determines beforehand that there will not be any results. My best guess here is that adding values_list to the end defeats the short-circuiting logic, allowing an actual query to be send, and that it’s actually values_list that determines the query that should be sent. Which in both cases is really the same, when you think about it. Either way you want to select just id on an unfiltered queryset.

    I emphasized that part, because I’m sure you’re jumping up and down now saying but none should return an empty queryset. True, but it does so by virtue of automatically returning an EmptyQuerySet and never actually querying the database at all. It doesn’t add any filters to the query.

    Whether this is a bug or not is debatable. I’m more apt to call this an edge-case that most likely can’t really be “fixed”. It’s a function of how all the interweaving parts come together in this one scenario.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently found the mobipick calendar and I really like it due to its
Recently i've found myself writing a lot of methods with what i can only
I just found out recently that you can configure Visual Studio (but this question
Recently, I found several web site have something like : Recommended for You, for
I recently found that we can use ?? operator to check nulls. Please check
I recently found out about livecoding where someone will program something on the fly
Recently I reviewed some C code and found something equivalent to the following: struct
recently found an article in http://hashtwo.com/blog/integrating-a-file-browser-into-ckeditor-cakephp to integrate a file browser into ckeditor (file
I recently found that dynamically creating object and methods in Ruby is quite a
I recently found javafx 2.1 very useful for my project of making a video

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.