使用 .order_by() 和 .latest() 进行 Django 查询
我有一个模型:
class MyModel(models.Model):
creation_date = models.DateTimeField(auto_now_add = True, editable=False)
class Meta:
get_latest_by = 'creation_date'
我的视图中有一个查询执行了以下操作:
instances = MyModel.objects.all().order_by('creation_date')
然后我想要 instances.latest()
,但它不会给我正确的实例,事实上它给了我第一个例子。仅当我将 order_by 设置为 -creation_date 或实际从查询中删除 order_by 时,.latest()
才给我正确的实例。当我使用 python manage.py shell 而不是在视图中手动测试时,也会发生这种情况。
所以我现在所做的是在模型的元中列出 order_by = ['creation_date']
,但没有在查询中使用它,并且这是有效的。
我希望 .latest()
始终返回基于(日期)(时间)字段的最新实例。谁能告诉我当您在查询中使用 order_by
时 .latest()
表现奇怪是否正确?
I have a model:
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')
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文档 说
所有这意味着,当您触发
MyModel.objects.latest()
时,您将获得基于日期/时间字段的最新实例。当我使用示例数据测试您的代码时,确实如此。您误解了
latest()
的工作方式。当调用 MyModel.objects 时,它返回表中的最新实例。当调用查询集时,
latest
将返回查询集中中的第一个对象。您的查询集包含按creation_date
升序排列的MyModel
的所有实例。很自然,此查询集上的最新
应该返回查询集的第一行。顺便说一句,这恰好是表中最旧的行。更好地理解的一种方法是查看针对
latest
触发的查询。情况 1:
此打印:
请注意按
creation_date DESC
和LIMIT
子句进行的排序。前者归功于get_latest_by
,而后者则是latest
的贡献。现在,情况 2:
打印
请注意,排序已更改为
creation_date ASC
。这是显式order_by
的结果。LIMIT
是附加在呃,稍后由latest
提供的。我们还可以看看情况 3:您为
objects.latest()
显式指定field_name
。节目
The documentation says that
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.You have misunderstood the way
latest()
works. When called on MyModel.objects it returnsthe 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 ofMyModel
ordered bycreation_date
in ascending order. It is only natural then thatlatest
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
.Case 1:
This prints:
Note the ordering by
creation_date DESC
and theLIMIT
clause. The former is thanks toget_latest_by
whereas the latter is the contribution oflatest
.Now, case 2:
prints
Note that the ordering has changed to
creation_date ASC
. This is the result of the explicitorder_by
. TheLIMIT
is tacked on er, later courtesylatest
.Let us also see Case 3: where you explicitly specify the
field_name
forobjects.latest()
.shows
我猜这是一个已知的 Django 中的 bug 在 1.3 版本后已修复被释放了。
I guess this is a known bug in Django that was fixed after 1.3 was released.
这对我有用
This worked for me
如果我们有 id 或日期的值,
它对我有用,即使缺少 id,它也会选择该值的下一个或上一个值
if we have the value of id or date
it worked for me, even if an id is missing it picks next or previous value to that