我有一个 Django 模型,DocumentComments
有两个日期时间字段created
和updated
. 我正在研究一个搜索函数,它解析搜索字符串并返回一个 Q 表达式,以DocumentComments
根据搜索字符串中的值查询模型。
我需要写一些类似的东西,日期时间字段中的年份在Q(created.year=xxxx)
哪里。但是,正如 Django 整个上午都在告诉我的那样,“关键字不能是表达式”。created.year
created
我尝试使用自定义模型管理器并使用年份字段注释默认查询集,但这不起作用,因为我似乎无法访问函数created.year
中的值get_queryset
。
class DocumentCommentManager(models.Manager):
def get_queryset(self):
c_year = self.created.year
u_year = self.updated.year
return super(DocumentCommentManager, self).get_queryset().annotate(created_year=c_year, updated_year=u_year)
我错过了什么,或者有什么更好的方法来实现我的目标?
谢谢!
标记