82๐
I havenโt tried this yet, but Iโd look at the latest() operator on QuerySets:
latest(field_name=None)
Returns the latest object in the
table, by date, using the field_name
provided as the date field.This example returns the latest Entry
in the table, according to the
pub_date field:Entry.objects.latest(โpub_dateโ)
If your modelโs Meta specifies
get_latest_by, you can leave off the
field_name argument to latest().
Django will use the field specified in
get_latest_by by default.Like get(), latest() raises
DoesNotExist if an object doesnโt
exist with the given parameters.Note latest() exists purely for
convenience and readability.
And the model docs on get_latest_by:
get_latest_by
Options.get_latest_by
The name of a DateField or DateTimeField in the model. This specifies the default field to use in your model Managerโs latest method.
Example:
get_latest_by = โorder_dateโ
See the docs for latest() for more.
Edit: Wade has a good answer on Q() operator.
51๐
this works!
Model.objects.latest('field')
โ field can be id. that will be the latest id
- [Django]-How do I include related model fields using Django Rest Framework?
- [Django]-Optimal architecture for multitenant application on django
- [Django]-Access request in django custom template tags
29๐
Since Django 1.6 โ last
last()
Works like first()
, but returns the last object in the queryset.
Returns the last object matched by the queryset, or None
if there is no matching object. If the QuerySet has no ordering defined, then the queryset is automatically ordered by the primary key.
list = List.objects.last()
gives you the last object created
- [Django]-Permission denied โ nginx and uwsgi socket
- [Django]-On Heroku, is there danger in a Django syncdb / South migrate after the instance has already restarted with changed model code?
- [Django]-How to force migrations to a DB if some tables already exist in Django?
20๐
For the largest primary key, try this:
List.objects.order_by('-pk')[0]
Note that using pk
works regardless of the actual name of the field defined as your primary key.
- [Django]-How to get the label of a choice in a Django forms ChoiceField?
- [Django]-What's the best solution for OpenID with Django?
- [Django]-DRY way to add created/modified by and time
4๐
You can use the count() method on a query set the get the number of items.
list = List.objects.all()
list.count()
Arguments to filter are โANDโed together. If you need to do OR filters look at Q objects.
http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
- [Django]-What are the differences between django-tastypie and djangorestframework?
- [Django]-Create a field whose value is a calculation of other fields' values
- [Django]-Django return file over HttpResponse โ file is not served correctly
0๐
alternative for the latest object created:
List.objects.all()[List.objects.count()-1]
It is necessary to add an AssertionError for the case when there are no items in the list.
except AssertionError:
...
- [Django]-Django 1.8 Run a specific migration
- [Django]-How to programmatically call a Django Rest Framework view within another view?
- [Django]-How do I go straight to template, in Django's urls.py?
0๐
I am working on Django version is 1.4.22, neither last
nor lastet
is working.
My way to solve with minimum db loading is like:
latest = lambda model_objects, field : model_objects.values_list( field, flat = True ).order_by( "-" + field )[ 0 ]
latest_pk = latest( List.objects, "pk" )
This function accepts query_set as input.
You may bind this function dynamically by doing:
import types
List.objects.latest = types.MethodType( latest, List.objects )
Then you should be able to get the last object by this latest pk easily.
- [Django]-Django-reversion and related model
- [Django]-ImportError: No module named 'django.core.urlresolvers'
- [Django]-How do I run tests against a Django data migration?
- [Django]-How do I restrict foreign keys choices to related objects only in django
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
- [Django]-How do I force Django to ignore any caches and reload data?