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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:52:27+00:00 2026-05-12T10:52:27+00:00

excuse me for my ugly english) ! Imagine these very simple models : class

  • 0

excuse me for my ugly english) !

Imagine these very simple models :

class Photo(models.Model):
    is_public = models.BooleanField('Public', default=False)

class Gallery(models.Model):
    photos = models.ManyToManyField('Photos', related_name='galleries', null=True, blank=True)

I need to select all Gallery instances which contain at least one public photo (and if possible adding a photos__count attribute which contains the number of public photos).

I tried this query :

Gallery.objects.all()\
    .annotate(Count('photos'))\
    .filter(photos__is_public=True)

It seems to be okay, but :
– the query is strange
– the added attribute photos__count on each gallery will contain the total number of photos on this gallery, instead of the number of public photos in this gallery.

I thin that the hard-coded sql query I need is that :

SELECT `gallery`.* , COUNT(`gallery_photos`.`photo_id`) 
FROM `gallery` 
    INNER JOIN `gallery_photos` ON (`gallery`.`id` = `gallery_photos`.`gallery_id`) 
    INNER JOIN `photo` ON (`gallery_photos`.`photo_id` = `photo`.`id`) 
WHERE `photo`.`is_public` = True 
GROUP BY gallery.id ;

Any idea to fix it ?

Thank you ! 😉

  • 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-12T10:52:27+00:00Added an answer on May 12, 2026 at 10:52 am

    The django documentation is at

    http://docs.djangoproject.com/en/dev/topics/db/aggregation/#order-of-annotate-and-filter-clauses

    and my experience says that the following query:

    Gallery.objects.filter(photos__is_public=True).annotate(Count('photos'))
    

    would give you galleries with at least one photo that is public and a count of only photos that are public. The only thing is that it will exclude galleries with zero public photos but it sounds like you don’t care about that. Have you tested the above query?

    If it still doesn’t return the right data then annotate is probably changing the returned data by causing it to return only galleries where all are public. In that case you could use the “extra” method to get the count you want.

    Gallery.objects.filter(photos__is_public=True).extra(select={
      "photo_count": """
      SELECT COUNT(`gallery_photos.id`)
          FROM `gallery_photos`
      WHERE `gallery_photos.gallery_id` `gallery.id AND
            `gallery_photos.is_public = True
      """})
    

    Jason Christa’s exclude method may also work.

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

Sidebar

Related Questions

Hello (please excuse me for my ugly english :p), Imagine these two simple models
Hi (please excuse me for my ugly english ;-) ) Do you know if
Excuse the simple question, the issue i'm having maybe due to lack of sleep.
Excuse my English, I speak Spanish, My question is how could I just click
Excuse me, I do not make a clear question in english. I need some
Excuse my ignorance, I'm very new to Python. I'm trying to perform factor analysis
Excuse me but I am very new at learning to use oracle and sql.
Excuse my English I'm French ^^ So, I've god a little question for you
Excuse the code dump, these are functions within a UserControl private void PNGQuantPreviewControl_Resize(object sender,
Excuse the newbness of this, I'm not getting very far relying on google and

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.