I want to get the model object with the lowest ID, in other words the oldest object in that field.
oldest = Model.objects.filter(id=lowest_number)
How can I achieve this?
You can use .first()
and .order_by()
.
oldest = Model.objects.order_by('id').first()
Or if you are sure that the queryset isn't empty then you can use slicing.
oldest = Model.objects.order_by('id')[0]
Have a look at the doc.
EDIT:
You can also use .earliest()
.
oldest = Model.objects.earliest('id')
Here's the doc.
See this question.