Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

399
Views
Django ORM data based on max date group

model

class quote(models.Model):
    id = models.AutoField(primary_key=True)
    stock = models.ForeignKey(stock, related_name='stock', on_delete=models.CASCADE)
    price = models.DecimalField(max_digits=30,decimal_places=5)
    valid_till = models.DateTimeField()
    created_at = models.DateTimeField() #time
    updated_at = models.DateTimeField(blank=True,null=True)

ex data

id stock price valid_till created_at
1. 1.    200.  09 April.   09 April 
2. 1.    300.  10 April.   09 April 
3. 2.    100.  07 April.   07 April 
4. 2.    200.  08 April.   08 April 

output desire

id stock price valid_till created_at
2. 1.    300.  10 April.   09 April 
4. 2.    200.  08 April.   08 April 

I need only record for max date of every stock

currently doing quote.objects.values('stock__symbol').annotate(max_date=Max('valid_till'))

but still not able to think about proper solution

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Here are two methods to get the quote(s) with max date. Using that queryset you can then find whatever related model instances you need.

  1. Find max value using aggregate() method, then query filtering the max value
# returns a dict containing your max_date
aggregation = quote.objects.aggregate(max_date=Max('valid_till'))

# get max_date from dict
max_date = aggregation['max_date']

#filter quotes based on max_date
max_date_quotes = quote.objects.filter(valid_till = max_date)
  1. Order by valid_till. This assumes you have a single max date.
max_date_quote = quote.objects.all().order_by('-valid_till').first()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!