Tell me how to make a request through Model.objects.get () or through Model.objects.filter () to get the last created model. There is a field
create_date = models.DateField( 'Создан', auto_now_add=True ) In the model in the meta you can define the field get_latest_by :
get_latest_by = "create_date" Then request
Model.objects.latest() will give you the latest model sorted by the create_date field, and the query
Model.objects.earliest() will issue the first created model.
Last created object
Model.objects.latest('create_date') model = Model.objects.all().order_by("create_date").last() PS: did not check. It translates to how to select all the models, sort them by date of creation and take the latest model from there.
Use a slice, django understands it correctly
Model.objects.all().order_by('-create_date')[:1] Source: https://ru.stackoverflow.com/questions/590823/
All Articles