문제

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.

도움이 되었습니까?

해결책

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')

다른 팁

Check the docs

Map.objects.order_by('date_created').first(10)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top