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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:58:46+00:00 2026-05-31T13:58:46+00:00

class Price(models.Model): date = models.DateField() price = models.DecimalField(max_digits=6, decimal_places=2) product = models.ForeignKey(Product) class Product(models.Model):

  • 0
class Price(models.Model):
    date = models.DateField()
    price = models.DecimalField(max_digits=6, decimal_places=2)
    product = models.ForeignKey("Product")

class Product(models.Model):
    name = models.CharField(max_length=256)
    price_history = models.ManyToManyField(Price, related_name="product_price", blank=True)

I want to query Product such that I return only those products for whom the price on date x is higher than any earlier date.

Thanks boffins.

  • 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-31T13:58:47+00:00Added an answer on May 31, 2026 at 1:58 pm

    As Marcin said in another answer, you can drill down across relationships using the double underscore syntax. However, you can also chain them and sometimes this can be easier to understand logically, even though it leads to more lines of code. In your case though, I might do something that would look this:

    first you want to know the price on date x:

    a = Product.objects.filter(price_history__date = somedate_x)
    

    you should probably test to see if there are more than one per date:

    if a.count() == 1:
        pass
    else:
        do something else here
    

    (or something like that)

    Now you have your price and you know your date, so just do this:

    b = Product.objects.filter(price_history__date__lt = somedate, price_history__price__gt=a[0].price)
    

    know that the slice will hit the database on its own and return an object. So this query will hit the database three times per function call, once for the count, once for the slice, and once for the actual query. You could forego the count and the slice by doing an aggregate function (like an average across all the returned rows in a day) but those can get expensive in their own right.

    for more information, see the queryset api:

    https://docs.djangoproject.com/en/dev/ref/models/querysets/

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

Sidebar

Related Questions

I have this model: class Journals(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code, max_length=50) name
class Author(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() friends = models.ManyToManyField('self', blank=True) class Publisher(models.Model):
I have a model like this. #models.py class Payment(models.Model): unit_price = models.DecimalField(max_digits=12, decimal_places=2) discount
For the following models: class Price: cad = models.DecimalField(max_digits=8, decimal_places=2) usd = models.DecimalField(max_digits=8, decimal_places=2)
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
I have a model like this: class Item(models.Model): code = models.CharField(max_length=200, unique=True) barcode =
My model is like below: class Manufacturers(models.Model): name = models.CharField() class Phones(models.Model): manufacturer =
I Have to tables class Book(models.Model): title = models.CharField(max_length=200) authors = models.ManyToManyField(Individual, related_name=author_for, blank=True,
I have this models: class BillHeader(models.Model): billno = models.IntegerField(primary_key=True, blank=True) class BillData(models.Model): price =
I have these models in Django: class Customer(models.Model): def __unicode__(self): return self.name name =

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.