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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:13:21+00:00 2026-05-22T03:13:21+00:00

I have three models, Contender, Week and Vote, each contender can have votes based

  • 0

I have three models, Contender, Week and Vote, each contender can have votes based on week,

class Contender(models.Model)
   week = models.ManyToManyField(Week)

class Week(models.Model):
   date_start = models.DateField()

class Vote(models.Model):
   contender = models.ForeignKey(Contender)
   week = models.ForeignKey(Week)

I would like to add something to the Contender, so I did this:

c = Count('vote', vote__week__date_start = "2011-01-03")
contenders = Contender.objects.all().annotate(vote_count=c).order_by('-vote_count')
contenders[0].vote_count

the problem is that when I add a vote with another Week (that has diferent date_start) the .vote_count value is changes and thus it seems like the extra parameters I pass to the Count object does not matter.

How do I do this type of annotation in 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-22T03:13:22+00:00Added an answer on May 22, 2026 at 3:13 am

    You could start from Vote:

    votes = Vote.objects.filter(week__date_start = "2011-01-03")      \
                        .values_list('contender')                     \
                        .annotate(cnt=Count('week')).order_by('-cnt')
    contender_pks = [d[0] for d in votes]
    contenders_dict = Contender.objects.in_bulk(contender_pks)
    
    contenders = []
    for pk, vote_count in votes:
        contender = contenders_dict[pk]
        contender.vote_count = vote_count
        contenders.append(conteder)
    

    Also, you can do some denormalization – add

    class VoteCount(models.Model):
       contender = models.ForeignKey(Contender)
       week = models.ForeignKey(Week)
       count = models.IntegerField(default=0)
    

    and count votes in it (overriding Vote.save() or using post_save signal), then you will just do:

    VoteCount.objects.filter(week__date_start = "2011-01-03") \
                     .select_related('contender')             \
                     .order_by('-count')
    

    It will be much more efficient performancewise if you do such statistics often.

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

Sidebar

Related Questions

No related questions found

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.