Django - Getting last object created, simultaneous filters Django - Getting last object created, simultaneous filters django django

Django - Getting last object created, simultaneous filters


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.


this works!

Model.objects.latest('field') - field can be id. that will be the latest id


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