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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:07:38+00:00 2026-05-17T16:07:38+00:00

I am looking to find a way to annotate a queryset with the counts

  • 0

I am looking to find a way to annotate a queryset with the counts of a subset of related items. Below is a subset of my models:

class Person(models.Model):
    Name = models.CharField(max_length = 255)
    PracticeAttended = models.ManyToManyField('Practice',
                                              through = 'PracticeRecord')

class Club(models.Model):
    Name = models.CharField(max_length = 255)
    Slug = models.SlugField()
    Members = models.ManyToManyField('Person')

class PracticeRecord(PersonRecord):
    Person = models.ForeignKey(Person)
    Practice = models.ForeignKey(Practice)

class Practice(models.Model):
    Club = models.ForeignKey(Club, default = None, null = True)
    Date = models.DateField()

I’m looking to make a queryset which annotates the number of club specific practices attended by a person. I can already find the total number of practices by that person with a query of Person.objects.all().annotate(Count('PracticeRecord'))

However I would like someway to annotate the number of practices that a person attends for a specific club.

I would prefer something using the django ORM without having to resort to writing raw SQL.

Thanks.

  • 1 1 Answer
  • 1 View
  • 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-17T16:07:38+00:00Added an answer on May 17, 2026 at 4:07 pm

    However I would like someway to annotate the number of practices that a person attends for a specific club.

    Let us see.

    First, find the specific club.

    club = Club.objects.get(**conditions)
    

    Next, filter all Persons who have practiced at this club.

    persons = Person.objects.filter(practicerecord__Practice__Club = club)
    

    Now, annotate with the count.

    q = persons.annotate(count = Count('practicerecord'))
    

    Edit

    I was able to successfully make this work in my test setup: Django 1.2.3, Python 2.6.4, Postgresql 8.4, Ubuntu Karmic.

    PS: It is a Good Idea™ to use lower case names for the fields. This makes it far easier to use the double underscore (__) syntax to chain fields. For e.g. in your case Django automatically creates practicerecord for each Person. When you try to access other fields of PracticeRecord through this field you have to remember to use title case.

    If you had used lower case names, you could have written:

    persons = Person.objects.filter(practicerecord__practice__club = club)
    #                                               ^^        ^^  
    

    which looks far more uniform.

    PPS: It is Count('practicerecord') (note the lower case).

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

Sidebar

Related Questions

Looking at the package javax.jws and related documentation I didn't find a way to
I am looking to find a way to programatically (C++) control/secure access to the
I'm looking to find a way to round up to the nearest 500.I've been
Hey i have been looking around and i cant find a way to get
I've tried to find a way to do this, but without success. I'm looking
Been looking around and can't find a definite way to do this... I need
I am looking for a way to find a Guid within a string that
we are looking for a way to find out (not modify, just find out)
I'm looking for a way to find/replace links to images (within user-generated content) without
I'm looking for an easy way to find the process tree (as shown by

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.