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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:29:04+00:00 2026-05-22T23:29:04+00:00

The model structure is Question has one Video, and Question has many Answers. The

  • 0

The model structure is Question has one Video, and Question has many Answers.

The problem query is:

questions = Question.objects\
  .values('id', 'answer', 'section__title', 'title', 'created_at','user__username')\
  .filter(video=v).annotate(answer_count=Count('answer'))

I’m using the Count aggregate function to add an extra field to say how many answers a question has.

It works fine for zero and one answers. But if a question has three answers, I get three different rows back with the same ID, with answer_count=1 and different IDs for the ‘answer’ field.

What am I doing wrong?

  • 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-22T23:29:05+00:00Added an answer on May 22, 2026 at 11:29 pm

    I’m not sure what your model definition is. I assume you have something like this:

    class Answer(models.Models):
        question = models.ForeignKey(Question, related_name='answer')
    

    When you query the way you described, you will retrieve one row per answer. If you leave out 'answer', from the values call, you should get what you wanted.

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

Sidebar

Related Questions

I had some questions regarding the structure and behavior of a model, using UML,
This is more of an open question rather than looking for one specific answer.
I got a lot of one-to-many relationships in my database structure. For example, you
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table
I have the following model structure: class Group < ActiveRecord::Base has_many :group_products, :dependent =>
I'm creating a text-based browser game and need some advice for django model structure.
I have the following structure: Model public class EventEntry : LogEntry { public EventType
I have a parent-child view model object structure set up and need to update
I'm trying to set up a custom sort behaviour for my model/view structure in

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.