2

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?

Zorgan
  • 8,227
  • 23
  • 106
  • 207

1 Answers1

2

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.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59