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')
Pregunta
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.
Solución
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')
Otros consejos
Check the docs
Map.objects.order_by('date_created').first(10)