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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:16:06+00:00 2026-06-13T23:16:06+00:00

I am using django annotate to display to users the rating and number of

  • 0

I am using django annotate to display to users the rating and number of reviews for a certain place. I have three models to do this:

class Descriptions(models.Model):
    name=models.CharField(max_length=50)
    website=models.CharField(max_length=50,blank=True)
    street1=models.CharField(max_length=50,blank=True)
    street2=models.CharField(max_length=50,blank=True)
    city=models.CharField(max_length=50,blank=True)
    state=models.CharField(max_length=50,blank=True)
    zip=models.CharField(max_length=5,blank=True)
    description=models.TextField()
    areas_related=models.TextField()
    add_area=models.CharField(max_length=50,blank=True)
    federal=models.NullBooleanField(null=True)
    lat=models.TextField(blank=True)
    long=models.TextField(blank=True)
    creator=models.ForeignKey(User)

    def __unicode__(self):
        return u'%s %s %s %s %s %s %s %s %s %s %s %s %s' %(self.name,self.website,self.street1,
        self.street2,self.city,self.state,self.zip,self.description,self.add_area, self.federal, self.lat, self.long, self.creator)

for the places. Then I have:

class Rating(models.Model):
    rating=models.IntegerField(blank=True)
    place=models.ForeignKey(Descriptions)

    def __unicode__(self):
        return u'%s %s' %(self.rating, self.place)

for the ratings. And finally:

class Review(models.Model):
    user=models.ForeignKey(User)
    place=models.ForeignKey(Descriptions)
    review=models.TextField(blank=True)

    def __unicode__(self):
        return u'%s %s %s' % (self.user, self.place, self.review)

And I am using this to get the places, along with ratings and reviews:

relevant=Descriptions.objects.annotate(Avg('rating')).annotate(Count('review'))

… However, I have a ratings system of 1 to 5. When I input a rating, I get a value of 16 for avg, which then increases by .5 a new rating is added. Also, the count for review increases by 3 every time I add a rating. So, something strange is going on, but I’m not sure exactly what it is, especially considering that number of reviews increases by 3 for any value of rating I put in… Any help would be appreciated.

  • 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-06-13T23:16:07+00:00Added an answer on June 13, 2026 at 11:16 pm

    Try:

    relevant=Descriptions \
        .objects \
        .annotate(average_rating = Avg('rating__rating')) \
        .annotate(review_count = Count('review'))
    

    Currently you are getting the average of the number of ratings objects, not the average number of rating values for each of those objects

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

Sidebar

Related Questions

Using Django 1.1.1 In models.py: class Site(models.Model): name = models.CharField(max_length=50) class SiteMonth(models.Model): site =
Using django-contact-form , I have overridden the form class: from contact_form.forms import ContactForm from
Using Django's built in models, how would one create a triple-join between three models.
using django 1.4 I have a model with a datetimefield. I imported django.utils.timezone to
I am using django-registration and would like to have a different page for failed
I'm using Django's post_save signal to send emails to users whenever a new article
I am using Django Crispy Forms together with Twitter-bootstrap and following this page: http://django-crispy-forms.readthedocs.org/en/d-0/tags.html#bootstrap-layout-objects
I have a problem that I would like to solve using Django's ORM. I
I using Django and a generic view django.views.generic.create_update.create_object I have a model form wich
I'm using Django 1.4 with Python 2.7 on Ubuntu 12.04. I have a view

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.