Question

I have a model class with two DateTimeField:

class Map(Models.Model):
    date_created = models.DateTimeField('date created', auto_now_add=True)
    date_updated = models.DateTimeField('date updated', auto_now=True)

I would like to have a query that returns the latest Map created and another query that returns the latest updated.

Was it helpful?

Solution

You can use latest():

latest(field_name=None)

Returns the latest object in the table, by date, using the field_name provided as the date field.

Map.objects.latest('date_created')
Map.objects.latest('date_updated')

OTHER TIPS

Check the docs

Map.objects.order_by('date_created').first(10)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top