Django Get Latest Entry from Database

时间:2015-06-26 10:03:42

标签: python django django-models

I've got 2 questions, but they are related to the same topic.

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

{% for status in status %}

    <tr>
        <td>{{ status.status}}</td>
    </tr>

{% endfor %}

However when I want to retrieve a single object i get an error even when i use:

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

and remove the for loop.

I get:

'Status' object is not iterable

My questions are:

  1. How can I get the latest entry from the database for a given model?
  2. How can I setup my templates tags to allow for just a single record?

5 个答案:

答案 0 :(得分:12)

You have two different questions here:

  1. How do I retrieve the latest object from the database.

You can do this using the latest() queryset operator. By reading the docs you will note that this operator works on date fields, not integers.

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

If you want to do this off the ID you will need to order by ID and select the first result. (this will only work if you are using incrementing primary keys, it will not work with UUID's or randomly generated hashes).

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

Side note: I would personally use the date_added / date_updated way of doing this.

  1. Iterating over a single object

A single object cannot be iterated over. For this you will need to use a different template. Or, you will need to add the single object into a list.

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

Personally I have a different views for listing single / multiple result. I have a ListView for many result objects and a DetailView for single objects.

答案 1 :(得分:1)

This is because latest returns a single instance rather than a queryset (which is iterable). So:

1) Latest is not working because it works with Date Fields. Read more at: 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 can't use for template tag with a single instance because it is not iterable.

To solve your situation, I would specify the ordering = ('id',) field in the Meta class of the model and then do a po = Status.objects.all()[:1] so you will obtain a queryset (which is iterable) with a single object in it. Then you will be able to use the for template tag with your po variable.

Hope it helps.

答案 2 :(得分:0)

让我们假设我有一个名为“ OneOfTheModelsUsed”的模型 该模型中有一个名为“ car_name”和“ date”的字段。

当我使用Django FormWizard时,以下代码对我有用。完成表单中的所有步骤后,将其保存。我用过

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

这将提供该模型中的所有条目

last_car_name = last_entry.car_name

这将以给定的形式提供您想要的特定字段条目。

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

将数据传递到模板。

在我使用的模板中显示

{{last_car_model}}

,如果需要该条目的ID,请在模板中使用此{{last_car_model.id}}

PS:从整体上来说,我对Django和Web开发还很陌生,所以我对这一切的技术术语并不了解

答案 3 :(得分:0)

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

“-date_filed'”字段将颠倒顺序,第一个字段将为您提供最新的元素。

答案 4 :(得分:-1)

comments = usercomment.objects.order_by('-id')[:100]

我是用这种方法做的

第一个“ comment”是变量名,“ usercomment”是模型名,“ id”是django在创建模型时生成的,[:100]从0到100,您可以将数字增加到无穷大