160👍
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.
- [Django]-Django AutoField with primary_key vs default pk
- [Django]-Django – Render the <label> of a single form field
- [Django]-How do I do an OR filter in a Django query?
65👍
Usign last():
ModelName.objects.last()
using latest():
ModelName.objects.latest('id')
- [Django]-Django Rest Framework model serializer with out unique together validation
- [Django]-Django admin ManyToMany inline "has no ForeignKey to" error
- [Django]-Django: guidelines for speeding up template rendering performance
21👍
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.
- [Django]-Constructing Django filter queries dynamically with args and kwargs
- [Django]-Django order_by query set, ascending and descending
- [Django]-Visual Editor for Django Templates?
- [Django]-What is the use of PYTHONUNBUFFERED in docker file?
- [Django]-How to compare two JSON objects with the same elements in a different order equal?
- [Django]-Removing 'Sites' from Django admin page
4👍
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’)
- [Django]-How to create an object for a Django model with a many to many field?
- [Django]-Factory-boy create a list of SubFactory for a Factory
- [Django]-Django 2 – How to register a user using email confirmation and CBVs?
3👍
You can do comparison with this down here.
latest('created')
is same as order_by('-created').first()
Please correct me if I am wrong
- [Django]-Using JSON in django template
- [Django]-Django gunicorn sock file not created by wsgi
- [Django]-Auto-create primary key used when not defining a primary key type warning in Django