Django从数据库获取最新条目

时间:2022-09-25 16:16:48

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循环中检索数据

{% 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.

并删除for循环。

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. 如何从给定模型的数据库中获取最新条目?

  3. How can I setup my templates tags to allow for just a single record?
  4. 如何设置模板标签以仅允许一条记录?

2 个解决方案

#1


11  

You have two different questions here:

这里有两个不同的问题:

  1. How do I retrieve the latest object from the database.
  2. 如何从数据库中检索最新对象。

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.

您可以使用latest()queryset运算符执行此操作。通过阅读文档,您将注意到此运算符适用于日期字段,而不是整数。

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).

如果您想通过ID执行此操作,则需要按ID排序并选择第一个结果。 (这只有在使用递增主键时才有效,它不适用于UUID或随机生成的哈希值)。

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

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

旁注:我个人会使用date_added / date_updated这样做的方式。

  1. Iterating over a single object
  2. 迭代单个对象

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.

就个人而言,我对列出单个/多个结果有不同的看法。我有一个用于许多结果对象的ListView和一个用于单个对象的DetailView。

#2


1  

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

这是因为latest返回单个实例而不是查询集(可迭代)。所以:

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.

1)最新版本无效,因为它适用于日期字段。欲了解更多信息,请访问:https://docs.djangoproject.com/en/1.8/ref/models/querysets/#latest。 'id'不是与最新过滤器一起使用的有效字段。

2) You can't use for template tag with a single instance because it is not iterable.

2)您不能将模板标签用于单个实例,因为它不可迭代。

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.

为了解决您的情况,我将在模型的Meta类中指定ordering =('id',)字段,然后执行po = Status.objects.all()[:1],这样您将获得一个查询集(其中是可迭代的,其中包含一个对象。然后,您将能够将po模板标记与po变量一起使用。

Hope it helps.

希望能帮助到你。

#1


11  

You have two different questions here:

这里有两个不同的问题:

  1. How do I retrieve the latest object from the database.
  2. 如何从数据库中检索最新对象。

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.

您可以使用latest()queryset运算符执行此操作。通过阅读文档,您将注意到此运算符适用于日期字段,而不是整数。

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).

如果您想通过ID执行此操作,则需要按ID排序并选择第一个结果。 (这只有在使用递增主键时才有效,它不适用于UUID或随机生成的哈希值)。

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

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

旁注:我个人会使用date_added / date_updated这样做的方式。

  1. Iterating over a single object
  2. 迭代单个对象

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.

就个人而言,我对列出单个/多个结果有不同的看法。我有一个用于许多结果对象的ListView和一个用于单个对象的DetailView。

#2


1  

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

这是因为latest返回单个实例而不是查询集(可迭代)。所以:

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.

1)最新版本无效,因为它适用于日期字段。欲了解更多信息,请访问:https://docs.djangoproject.com/en/1.8/ref/models/querysets/#latest。 'id'不是与最新过滤器一起使用的有效字段。

2) You can't use for template tag with a single instance because it is not iterable.

2)您不能将模板标签用于单个实例,因为它不可迭代。

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.

为了解决您的情况,我将在模型的Meta类中指定ordering =('id',)字段,然后执行po = Status.objects.all()[:1],这样您将获得一个查询集(其中是可迭代的,其中包含一个对象。然后,您将能够将po模板标记与po变量一起使用。

Hope it helps.

希望能帮助到你。