你好,我正在尝试使用Django的annotate功能,我认为我做得很好,但是我一定漏掉了什么,因为我遇到了Attribute Error错误。
这是我的模型:
class Operation(models.Model):
...
class Message(models.Model):
operation = models.ForeignKey(Operation)
sent_on = models.DateTimeField(auto_now_add=True)
...
以下是我尝试做的事情:
ops = Operation.objects.filter(...)
ops.annotate(last_message=Max('message__sent_on'))
ops.order_by('-last_message')
print ops[0].last_message
我正在获得
AttributeError at ...
'Operation' object has no attribute 'last_message'
请注意,如果我将注释更改为
ops.annotate(last_message=Max('whatever'))
,我会收到FieldError
,因此之前的语法是正确的...但为什么我无法访问last_message字段呢?
我正在使用django 1.6.10
谢谢!