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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:36:14+00:00 2026-06-04T07:36:14+00:00

I have 3 models in my django app class Company(models.Model): name = models.CharField(_(Name), max_length

  • 0

I have 3 models in my django app

class Company(models.Model):

    name = models.CharField(_("Name"), max_length = 100, blank = True, null = True)
    address = models.CharField(_("Address"), max_length = 300, blank = True, null = True)

class WebSite(models.Model):

    WEBSITE_TYPE_CHOICES = (
        ('Google+', 'Google+'),
        ('Facebook', 'Facebook'),
        ('Orkut', 'Orkut'),
    )
    user = models.ForeignKey(User)
    website = models.URLField(blank = True, null = True)
    website_type = models.CharField(max_length = 15, choices = WEBSITE_TYPE_CHOICES, default = 'Google+')
    company = models.ForeignKey(Company, null=True, blank=True)

class Review(models.Model):

    rating = models.FloatField(blank = True, null = True) 
    review = models.TextField(blank = True, null = True)
    company = models.ForeignKey(Company, null=True, blank=True)
    website = models.ForeignKey(Website, null=True, blank=True)

I want to display my data such as

CompanyA:

  1. Google – 5 reviews, Avg 3.4
  2. Orkut – 9 reviews, Avg 4.1
  3. Facebook – 12 reviews, Avg 4.3
  4. Anyother – 2 reviews, Avg 2

CompanyB:

  1. Google – 2 reviews, Avg 3
  2. Facebook – 5 reviews, Avg 4

For this I have done

    final_list = company_list = []
    websites = Website.objects.select_related().filter(user = user)

    for website in websites:
        company = website.company
        if company is not None and company not in company_list:
            company_list.append(brand)
            company_websites = websites.filter(company = company)
            info = []
            for company_website in company_websites:
                company_type = company_website.website_type
                reviews = Review.objects.filter(company = company, website = company_website)
                no_of_reviews = len(reviews)
                average = reviews.aggregate(Avg('rating'))
                info.append({"type": company_type, "no_of_reviews": no_of_reviews,
                             "average": average['rating__avg']})
            final_list.append({"company": company.name, "stats": info})

There are 2 problems with the code

  1. It is wasting a lot of computation
  2. It also somehow appends empty lists in the final_list (BUGGY)

FYI: I believe .annotate() can be used, but I fail to understand how can I use it based on a column, which in my case is (company__name)

  • 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-04T07:36:15+00:00Added an answer on June 4, 2026 at 7:36 am

    This will give you the data.

    Review.objects.values(company, website__website_type).annotate(review_count=Count('id'), average=Avg('rating')).order_by('company')
    

    Use {% regroup %} to output it in template.

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

Sidebar

Related Questions

In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)
I have these Django models: class Group(models.Model): name = models.CharField(max_length=100) parent_group = models.ManyToManyField("self", blank=True)
I have the following model in my Django app: class Group(models.model): name=models.CharField(max_length=30) users=Models.ManyToManyField(User) In
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have an existing app with the following model class Contact(models.Model): lastname = models.CharField(max_length=200)
I've got a basic Item model in my Django app: class Item(models.Model): name =
Lets say, I have following models in my Django app. class EventGroup name =
I have a django app with models as follows: A Question model An Answer
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_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.