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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:12:39+00:00 2026-05-26T20:12:39+00:00

Based off this SO question about counting forgein keys , I have a problem.

  • 0

Based off this SO question about counting forgein keys, I have a problem. This is my models:

class Type(models.Model):
  is_bulk = models.BooleanField()

class Component(models.Model):
  parent = models.ForgeinKey(Type, related_name="components")

I want to write a queryset that has all types, except those that have is_bulk=True and have no components. If is_bulk=False, it should be included. If is_bulk=True and you have 1+ linked Components, then you’re included. If not, you’re excluded.

Based off the answer, I tried this queryset:

Type.objects.annotate(num_components=Count('components')).exclude(is_bulk=True, num_components=0)

But it returns no results.

However this implies that there should be results:

>>> [(x.is_bulk, x.num_components) for x in Type.objects.annotate(num_components=Count('components'))]
[(False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0)]

All the Type objects have is_bulk=False and all of them have 0 Components. From reading the .exclude(…) documentation, it should be NOT(is_bulk=True AND num_components=0), which should be True for every Type. Right? (Have I made a misunderstanding here, if so, what’s the correct queryset)

If not, why is this queryset returning [], when it should return all of them? Is this a bug in Django 1.3?

  • 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-26T20:12:40+00:00Added an answer on May 26, 2026 at 8:12 pm

    I have fixed this by changing the queryset to:

    types_qs.annotate(num_components=Count('components')).filter(Q(is_bulk=False) | (Q(is_bulk=True) & Q(num_components__gt=0)))
    

    That works, but I can’t see why my original one didn’t. Ah well.

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

Sidebar

Related Questions

So, my second question based off of this application I'm teaching myself Objective C
This question is being based off of a similar question titled, ASP.NET MVC Relative
I posted a question about this earlier, but I have more information now and
I have POSIX in mind for implementation, though this question is more about architecture.
As a follow up to this previous question , I have a Core Data-based
this is a question about which class does a free on shared pointers. So
This is kind of based off of this question . I'm currently looking at
I tried to make an accordion effect with JavaScript based off this video altering
I have an iphone app that I built based off a tutorial (for a
So I have a fairly simple vertical CSS menu based off of UL. <ul

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.