Django查询使用.order_by()和.latest() [英] Django Query using .order_by() and .latest()

查看:1420
本文介绍了Django查询使用.order_by()和.latest()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型:

class MyModel(models.Model):
   creation_date = models.DateTimeField(auto_now_add = True, editable=False)

   class Meta:
      get_latest_by = 'creation_date'

我在我看来有一个查询,执行以下操作:

I had a query in my view that did the following:

instances = MyModel.objects.all().order_by('creation_date')

然后我想要 .latest(),但它不会给我正确的实例,实际上它给了我一个实例。只有当我将order_by设置为 -creation_date 或者实际上从查询中删除了order_by, .latest()给我正确的实例。当我使用python manage.py shell而不是在视图中手动测试时,也会发生这种情况。

And then later I wanted instances.latest(), but it would not give me the correct instance, in fact it gave me the first instance. Only when I set order_by to -creation_date or actually removed the order_by from the query did .latest() give me the correct instance. This also happens when I test this manually using python manage.py shell instead of in the view.

所以我现在所做的是在模型的Meta中列出 order_by = ['creation_date'] 而不是在查询中使用,并且可以使用。

So what I've done now is in the Model's Meta I've listed order_by = ['creation_date'] and not used that in the query, and that works.

预期 .latest()总是基于(日期)(时间)字段返回最近的实例。任何人都可以告诉我,当您在查询中使用 order_by 时, .latest()是否正常行为是否正确? p>

I would have expected .latest() to always return the most recent instance based on a (date)(time) field. Could anyone tell me whether it's correct that .latest() behaves strangely when you use order_by in the query?

推荐答案


我希望.latest()总是返回最近的一个基于)(时间)字段。

I would have expected .latest() to always return the most recent instance based on a (date)(time) field.

文档


如果您的模型的 Meta 指定 get_latest_by ,您可以将 field_name 参数留给最新()。默认情况下,Django将使用 get_latest_by 中指定的字段。

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.

是当你发起 MyModel.objects.latest()时,你会根据日期/时间字段获得最新的实例。当我使用样本数据测试你的代码时,确实是这样做的。

All this means is that when you fire MyModel.objects.latest() you will get the latest instance based on the date/time field. And when I tested your code using sample data, it indeed did.


然后,我想要instances.latest(),但它会不给我正确的例子,其实它给了我一个例子。

And then later I wanted instances.latest(), but it would not give me the correct instance, in fact it gave me the first instance.

你误解了 latest()的作品。当在 MyModel.objects 上调用时,它返回
中的最新实例。当调用查询集时,最新的将返回查询集中的第一个对象。您的查询器由 creation_date 按升序排列的所有 MyModel 实例组成。那么这个queryset的最新的是自然而然的,应该返回查询集第一个。这恰好恰好是中最旧的一行。

You have misunderstood the way latest() works. When called on MyModel.objects it returns the latest instance in the table. When called on a queryset, latest will return the first object in the queryset. Your queryset consisted of all instances of MyModel ordered by creation_date in ascending order. It is only natural then that latest on this queryset should return the first row of the queryset. This incidentally happens to be the oldest row in the table.

获得更好理解的一种方法是查看针对最新

One way to get a better understanding is to view the query fired for latest.

案例1:

from django.db import connection
MyModel.objects.latest()
print connection.queries[-1]['sql']

打印:

SELECT "app_mymodel"."id", "app_mymodel"."creation_date" FROM 
"app_mymodel" ORDER BY "app_mymodel"."creation_date" DESC LIMIT 1

注意 creation_date DESC LIMIT 子句的排序。前者是感谢 get_latest_by ,而后者是最新的的贡献。

Note the ordering by creation_date DESC and the LIMIT clause. The former is thanks to get_latest_by whereas the latter is the contribution of latest.

现在,情况2:

MyModel.objects.order_by('creation_date').latest()
print connection.queries[-1]['sql']

打印

SELECT "app_mymodel"."id", "app_mymodel"."creation_date" FROM 
"app_mymodel" ORDER BY "app_mymodel"."creation_date" ASC LIMIT 1

请注意,订单已更改到 creation_date ASC 。这是明确的 order_by 的结果。 LIMIT 在呃,以后礼貌最新的

Note that the ordering has changed to creation_date ASC. This is the result of the explicit order_by. The LIMIT is tacked on er, later courtesy latest.

我们还可以看到案例3:您明确指定对象的 field_name 。最新()

Let us also see Case 3: where you explicitly specify the field_name for objects.latest().

MyModel.objects.latest('id')
print connection.queries[-1]['sql']

显示

SELECT "app_mymodel"."id", "app_mymodel"."creation_date" FROM "app_mymodel"
ORDER BY "app_mymodel"."id" DESC LIMIT 1

这篇关于Django查询使用.order_by()和.latest()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆