问题描述
我正在使用注释为对象添加属性,然后可以将其用于order_by。但是,我想在一个关系的字段上注释一个关系。我知道我应该可以使用双下划线表示法进入该领域,但是我似乎无法将其包裹住。
I'm using annotate to add a property to an object which I can then use for order_by. However, I want to annotate on a field of a relation on a relation. I know I should be able to get to the field somehow using double-underscore notation, but I just can't seem to wrap my head around it.
这里是模型:
class Group(Taggable, Uploadable):
name = models.CharField(max_length=250, db_index=True)
description = models.TextField(max_length=5000, null=True,
blank=True, db_index=True)
private = models.BooleanField(default=False)
members = models.ManyToManyField(User, null=True,
related_name='members', through='GroupToUser')
pending_members = models.ManyToManyField(User, null=True,
related_name='pending_members')
admin = models.ForeignKey(User, null=True)
timestamp = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(User, related_name='author')
class Discussion(Taggable, Uploadable):
author = models.ForeignKey(User)
title = models.CharField(max_length=250, db_index=True)
description = models.TextField(max_length=5000, null=True,
blank=True, db_index=True)
group = models.ForeignKey(Group, null=True)
timestamp = models.DateTimeField(auto_now_add=True)
class DiscussionResponse(Uploadable):
author = models.ForeignKey(User)
discussion = models.ForeignKey(Discussion)
message = models.TextField(max_length=5000)
timestamp = models.DateTimeField(auto_now_add=True)
因此,可以有选择地关联讨论一个组,然后DiscussionRespons与一个讨论相关联。我想做的是找到与组相关的所有讨论的最新 ResponseResponse(如果存在),然后按此排序。
So, a Discussion can optionally be associated with a Group, and DiscussionResponses are associated with a discussion. What I would like to do is find the most recent DiscussionResponse on any discussions connected to a Group, if it exists, and sort by that.
我已经知道了这样:
Group.objects.filter(some_filtering).distinct().annotate(
last_response=Max('some__reverse__relationship__timestamp').order_by(
'-last_response')
我只是不能
UPDATE:
在这种情况下,似乎可以找到正确的方法来获取讨论响应的时间戳。例如,下面是一个带有相关讨论时间戳记的 order_by
的示例:
>>> groups = Group.objects.all().annotate(
last_response=Max('discussion__timestamp')).order_by('-last_response')
>>> for group in groups:
... print(group.id, group.last_response)
...
...
(2L, datetime.datetime(2013, 5, 8, 15, 32, 31))
(1L, None)
(3L, None)
(4L, None)
(6L, None)
(7L, None)
(8L, None)
(9L, None)
在这种情况下,只有#2组具有相关的讨论,因此它移至顶部;其余的保留自然顺序。不过,我真正想做的是将最近对讨论进行了回复的移动组移到列表的顶部。这就是为什么我认为'discussion__discussionresponse__timestamp'
可以工作,但似乎不起作用的原因。
In this case, only group #2 has related discussions so it was moved to the top; the rest retain the natural order. What I'd really like to do, though, is move groups that have recent responses to discussions moved to the top of the list. That's why I thought 'discussion__discussionresponse__timestamp'
would work, but it doesn't seem to.
推荐答案
好吧,很明显,是只是 $ disccussion__discussionresponse__timestamp 。我在Django shell中进行了尝试,保存了一个新的DiscussionResponse后,它不起作用,但是几分钟后再次尝试时,它起作用了:
Ok, apparently it is just 'discussion__discussionresponse__timestamp'
. I tried it in the Django shell and it didn't work after saving a new DiscussionResponse, but it worked several minutes later when I tried it again:
>>> groups = Group.objects.all().annotate(last_response=Max(
'discussion__discussionresponse__timestamp')).order_by('-last_response')
>>> for group in groups:
... print(group.id, group.last_response)
...
...
(2L, datetime.datetime(2013, 5, 16, 14, 56, 22))
(1L, None)
(3L, None)
(4L, None)
(6L, None)
(7L, None)
(8L, None)
(9L, None)
>>>
如果有人知道为什么在将新对象保存到数据库后它不起作用,但是
If anyone knows why it didn't work right after saving a new object to the database, but did work later, that would probably be useful information.
这里是查询的另一次运行,其中讨论/响应添加到另一个组中只是为了进行验证:
Here's another run at the query with discussions/responses added to another group just for added verification:
>>> groups = Group.objects.all().annotate(last_response=Max('discussion__discussionresponse__timestamp')).order_by('-last_response')
>>> for group in groups:
... print(group.id, group.last_response)
...
...
(4L, datetime.datetime(2013, 5, 16, 15, 25, 40))
(2L, datetime.datetime(2013, 5, 16, 15, 16, 46))
(1L, None)
(3L, None)
(6L, None)
(7L, None)
(8L, None)
(9L, None)
>>>
这篇关于Django:如何在相关字段的相关字段上使用order_by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!