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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:00:16+00:00 2026-05-13T21:00:16+00:00

I need some help putting together this query in Django. I’ve simplified the example

  • 0

I need some help putting together this query in Django. I’ve simplified the example here to just cut right to the point.

MyModel(models.Model):
    created = models.DateTimeField()
    user = models.ForeignKey(User)
    data = models.BooleanField()

The query I’d like to create in English would sound like:

Give me every record that was created yesterday for which data is False where in that same range data never appears as True for the given user

Here’s an example input/output in case that wasn’t clear.

Table Values

ID   Created    User    Data

1    1/1/2010   admin   False
2    1/1/2010   joe     True
3    1/1/2010   admin   False
4    1/1/2010   joe     False
5    1/2/2010   joe     False

Output Queryset

1    1/1/2010   admin   False
3    1/1/2010   admin   False

What I’m looking to do is to exclude record #4. The reason for this is because in the given range “yesterday”, data appears as True once for the user in record #2, therefore that would exclude record #4.

In a sense, it almost seems like there are 2 queries taking place. One to determine the records in the given range, and one to exclude records which intersect with the “True” records.

How can I do this query with the Django ORM?

  • 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-13T21:00:16+00:00Added an answer on May 13, 2026 at 9:00 pm

    You don’t need a nested query. You can generate a list of bad users’ PKs and then exclude records containing those PKs in the next query.

    bad = list(set(MyModel.obejcts.filter(data=True).values_list('user', flat=True)))
    # list(set(list_object)) will remove duplicates
    # not needed but might save the DB some work
    
    rs = MyModel.objects.filter(datequery).exclude(user__pk__in=bad)
    # might not need the pk in user__pk__in - try it
    

    You could condense that down into one line but I think that’s as neat as you’ll get. 2 queries isn’t so bad.

    Edit: You might wan to read the docs on this:

    http://docs.djangoproject.com/en/dev/ref/models/querysets/#in

    It makes it sound like it auto-nests the query (so only one query fires in the database) if it’s like this:

    bad = MyModel.objects.filter(data=True).values('pk')
    rs  = MyModel.objects.filter(datequery).exclude(user__pk__in=bad)
    

    But MySQL doesn’t optimise this well so my code above (2 full queries) can actually end up running a lot faster.

    Try both and race them!

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

Sidebar

Ask A Question

Stats

  • Questions 337k
  • Answers 337k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When I installed it I didn't change any defaults and… May 14, 2026 at 3:59 am
  • Editorial Team
    Editorial Team added an answer One might think that $('ul[actualpage]').attr('newactualpage',$(this).attr('actualpage')) is the answer. However, this… May 14, 2026 at 3:59 am
  • Editorial Team
    Editorial Team added an answer Deleting all characters like: , : - / and leaving… May 14, 2026 at 3:59 am

Related Questions

I'm relatively new to jquery, so I have what I hope will be a
I'm interested in putting together my first online game using Flash as the client
I am trying to make form elements fit into a visual style with other
So I am trying to invoke methods on a page and I know the
i'd appreciate some help putting together a sql script to copy data from one

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.