Django Get last record from database

I have 2 questions, but they are related to the same topic.

I know how to retrieve data from for loop using template tags

 {% for status in status %} <tr> <td>{{ status.status}}</td> </tr> {% endfor %} 

However, when I want to get one object, I get an error message even when I use:

 po = Status.objects.latest('id') 

and remove the for loop.

I get:

 'Status' object is not iterable 

My questions:

  • How can I get the latest record from the database for this model?
  • How to set up template tags for one record?
+7
source share
4 answers

You have two different questions here:

  1. How to get the last object from the database.

You can do this using the latest() query statement. When reading documents, you will notice that this operator works with date fields, not with integers.

 Status.objects.latest('date_added') # or date_updated 

If you want to do this without an identifier, you will need to order by identifier and select the first result. (this will only work when using incremental primary keys, it will not work with UUIDs or randomly generated hashes).

 Status.objects.order_by('id')[0] 

Note: I would personally use date_added/date_updated .

  1. Iterate over a single object

One object cannot be repeated. You will need to use a different template for this. Or you will need to add one object to the list.

 # note the [] around the query result = [Status.object.latest('date_added')] 

Personally, I have different views for listing one / several results. I have a ListView for many result objects and a DetailView for individual objects.

+15
source

This is because latest returns a single instance, not a set of queries (which iterates over). So:

1) The latter does not work, because it works with the Date fields. More details: https://docs.djangoproject.com/en/1.8/ref/models/querysets/#latest . 'id' is not a valid field to use with the latest filter.

2) You cannot use the for template tag with a single instance because it is not iterable.

To solve your situation, I have to specify the ordering = ('id',) field in the Meta class of the model, and then do po = Status.objects.all()[:1] so that you get a request (which is iterable) with one object in him. You can then use the for template tag with the variable po .

Hope this helps.

+1
source

Suppose I have a model named "OneOfTheModelsUsed", and in this model there is a field named "car_name" and "date".

The following code worked for me when I used Django FormWizard. After going through all the steps in the form, and it will be saved. I used

 last_entry = OneOfTheModelsUsed.objects.latest("date") 

This gives all the entries in this model.

 last_car_name = last_entry.car_name 

This gives the specific field entry that you want in the given form.

 return render(request, 'reference.html', {'last_car_name':last_car_name,} 

passed the data to the template.

to display in the template that I used

 {{last_car_model}} 

and if you need an identifier for this entry .. use this {{last_car_model.id}} in the template.

PS: I'm pretty new to Django and web development in general, so I don’t know a lot of technical terms for all this

0
source
 TableName.objects.filter(key=value).order_by('-date_filed').first() 

The "-date_filed" field changes the order, and the first one will give you the last item.

0
source

All Articles