ChatGPT解决这个技术问题 Extra ChatGPT

Get the latest record with filter in Django

I am trying to get the latest Django model object but cannot seem to succeed.

Neither of these are working:

obj = Model.objects.filter(testfield=12).latest()
obj = Model.objects.latest().filter(testfield=12)
Have you tried: obj= Model.objects.filter(testfield=12).order_by('-id')[:1]
@catherine, working like a charm! :D. you want to write this as answer so i can check it?
In 2018 NewsPostImage.objects.filter(newsPostTarget=img_id).first() . Hope it helps.

P
Pranav Singh

See the docs from django: https://docs.djangoproject.com/en/dev/ref/models/querysets/#latest

You need to specify a field in latest(). eg.

obj= Model.objects.filter(testfield=12).latest('testfield')

Or if your model’s Meta specifies get_latest_by, you can leave off the field_name argument to earliest() or latest(). Django will use the field specified in get_latest_by by default.


Does "testfield" have to be a date field?
c
catherine
obj= Model.objects.filter(testfield=12).order_by('-id')[0]

@DaveMerwin it's not incorrect; there are multiple ways of solving this problem, and both this answer and the other are correct.
Note this makes the assumption that id is a sequenced primary key integer. Which is normally the default for django, but sometimes not.
This could be dangerous because filter could return an empty list
'-id' is used to reverse the order!
maybe .first() instead of [0] ?
S
Subin Shrestha

last() latest()

Usign last():

ModelName.objects.last()

using latest():

ModelName.objects.latest('id')

a
acjay

latest is really designed to work with date fields (it probably does work with other total-ordered types too, but not sure). And the only way you can use it without specifying the field name is by setting the get_latest_by meta attribute, as mentioned here.


It does work with primary keys, you can always do something like this: Model.objects.latest('id')
T
Thomas John

obj= Model.objects.filter(testfield=12).order_by('-id')[:1] is the right solution


N
Nikhil Bhardwaj

You can do comparison with this down here.

https://i.stack.imgur.com/49QK0.png

latest('created') is same as order_by('-created').first() Please correct me if I am wrong


C
Cornel Ciobanu
obj = Model.objects.filter(testfield=12).order_by('id').latest('id')

Filter based on what field you need - in this case is testfield

Model.objects.filter(testfield=12)

In order to get the latest record, first you need to sort the queryset. So that it knows to return the last record based on a criteria.

Now, order the results base on your primary key - most of the times the pk=id

Model.objects.filter(testfield=12).order_by('id')

https://docs.djangoproject.com/en/4.0/ref/models/querysets/#order-by

By default, results returned by a QuerySet are ordered by the ordering tuple given by the ordering option in the model’s Meta. You can override this on a per-QuerySet basis by using the order_by method.

After you have the queryset and you have sorted it. Get the latest based on the criteria you need - in this case is the id.

Model.objects.filter(testfield=12).order_by('id').latest('id')


Thanks for contributing! If you find time please edit an explanation into the answer as it's recommended to explain entirely code-based answers on StackOverflow.