Python Django:如何在相关字段的相关字段上排序

Python Django:如何在相关字段的相关字段上排序,python,django,sql-order-by,annotate,Python,Django,Sql Order By,Annotate,我正在使用annotate向对象添加一个属性,然后可以将其用于order_by。但是,我想在一个关系的字段上注释一个关系。我知道我应该能够以某种方式使用双下划线符号到达字段,但我似乎无法将我的头围绕它 以下是模型: class Group(Taggable, Uploadable): name = models.CharField(max_length=250, db_index=True) description = models.TextFiel

我正在使用annotate向对象添加一个属性,然后可以将其用于order_by。但是,我想在一个关系的字段上注释一个关系。我知道我应该能够以某种方式使用双下划线符号到达字段,但我似乎无法将我的头围绕它

以下是模型:

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)
因此,可以选择将讨论与小组相关联,而DiscussionResponse与讨论相关联。我想做的是找到与某个小组有关的任何讨论(如果存在)的最新讨论回应,并按其排序

我已经做到了这一点:

Group.objects.filter(some_filtering).distinct().annotate(
    last_response=Max('some__reverse__relationship__timestamp').order_by(
        '-last_response')
在这种情况下,我似乎无法找到正确的方法来获取DiscussionResponse上的时间戳

更新: 您确实可以按带注释的值排序。下面是一个例子,在相关讨论的时间戳上有一个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\uu discussionresponse\uu timestamp'
会起作用,但它似乎不起作用。

好吧,显然它是只是
'discussion\udiscussionresponse\uu timestamp'
。我在Django shell中尝试了它,但在保存新DiscussionResponse后,它不起作用,但几分钟后,当我再次尝试时,它起了作用:

>>> 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)
>>> 
如果有人知道为什么在将一个新对象保存到数据库后它不能正常工作,但后来又能正常工作,那么这可能是有用的信息

下面是另一次运行查询,将讨论/响应添加到另一个组中,仅用于添加验证:

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

我尝试过以下方法,但没有一种有效:
讨论\讨论响应\时间戳
讨论\设置\讨论响应\时间戳
,以及
讨论集\讨论响应\时间戳
,我不明白你试图实现什么。例如,您声明将给您一个时间戳的
last\u response=Max(…)
,然后按此值排序,您必须按列排序,而不是按值排序,对吗?您需要的是按时间戳列排序??您可以对带注释的值
order\u by
;我以前用过这个把戏。问题是,我似乎无法找到与组相关的讨论相关的discussionresponse的时间戳。您的字段查找字符串将是
discussion\uu discussionresponse\u timestamp
,请注意时间戳前的一个下划线。你在这么做吗?在更新中,在时间戳之前显示两个下划线。@PauloBu一个下划线不起作用,至少在Django 1.4:
FieldError:无法将关键字“discussionresponse\u timestamp”解析到字段中。选择包括:作者、描述、讨论响应、文件url、组、id、图像高度、图像url、图像宽度、mimetype、标记、时间戳、标题