-1
class Avergae(models.Model):
    state = models.ForeignKey(State, on_delete=models.CASCADE)
    region = ChainedForeignKey(Region, chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
    cluster = ChainedForeignKey(Cluster, chained_field="region",chained_model_field="region", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
    school = ChainedForeignKey(School, chained_field="cluster",chained_model_field="cluster", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
    average_date = models.DateField()
    average = models.DecimalField(max_digits=4, decimal_places=2)
    attendance = models.IntegerField()
    note = models.CharField(max_length=250)

def __str__(self):
    return '{}: {}'.format(self.average_date, self.average)

I wanted to filter the latest object according to the date. Needed to get the object that has most recent date value for attribute average_date. Please let me know the syntax. Thank you.

venkat mani sai
  • 89
  • 1
  • 3
  • 11
  • what approach/s have you tried so far? how this fits into your app? It would help if you could add the details abt how your app interacts with db to fetch the list of objects, size of data. – Rishi Sep 19 '17 at 21:52

1 Answers1

2

You could use queryset method latest to get the latest object by average_date:

obj = Avergae.objects.latest('average_date')
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164