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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:01:49+00:00 2026-06-08T10:01:49+00:00

I am running a filter query in Django with range . I thought filter

  • 0

I am running a filter query in Django with range. I thought filter always returned a queryset that was grouped by the primary key, but it seems not.

These are my models:

class MCode(models.Model):
    code = models.CharField(max_length=16)
class Product(models.Model):
    id = models.CharField(max_length=40, primary_key=True, db_index=True)
    mcode = models.ForeignKey(MCode, null=True, blank=True, db_index=True)
class Review(models.Model):
    review_id = models.CharField(max_length=32, primary_key=True, db_index=True) 
    product = models.ForeignKey(Product, db_index=True) 
    rating = models.IntegerField() 
    time = models.DateTimeField(db_index=True) 

And this is my code:

mcode = 'M83'
base = Product.objects
tcode_obj = MCode.objects.filter(code=mcode.upper())
return base.filter(tcode=tcode_obj, 
                   review__time__range=[date_from, date_to])

I’m getting five results, but three of them have the same primary key. It looks like I’m getting a result for each review, not each product.

Does anyone know how could I group these products by ID, and annotate with the count of the attached reviews?

  • 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-08T10:01:51+00:00Added an answer on June 8, 2026 at 10:01 am

    When you select based on something that could match multiple times, such as Product with Reviews during a range of time, each match is added to the queryset, even if it’s a duplicate of an item already there. There’s actually times where you might want this behavior, but if you need to limit the queryset to just unique items, use .distinct() at the end of your query. Getting duplicates is also common when using logical ORs in your query, so be aware of times you do that and remember to use .distinct() there as well.

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

Sidebar

Related Questions

I have situation, where running a query that filters by an indexed column in
I am running a query using Linq2SQL that comes down to following query: DateTime?
I'm attempting to query AD in an ASP.Net (4.0) application that is running on
I have @articles in my database and I want to query it but filter
I am running the following MySQL query to filter the number of listings with
I've had a query that has been running fine for about 2 years. The
Currently I can not quickly filter the result of a work-item query - running
I have a regex I'm running to filter rows in a table. The filtering
I keep running into the following problem: (System.Console.ReadLine ()).Split [|'('; ')'|] |> Array.filter (fun
Running my script through Devel::NYTProf showed that the following portion of code took up

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.