【发布时间】:2018-02-22 16:42:42
【问题描述】:
我试图通过 VOTES - POST AGE 进行 django 查询以获取帖子顺序。
示例:
普通帖
1 - Hello World (10 votes) (Posted today)
2 - Stackoverflow ( 12 votes) (Posted 2 days ago)
3 - StackExchange ( 30 votes ) (Posted 19 days ago)
所以当我通过注释过滤查询时,我想要这个:
1 - StackExchange ( 30 votes - 19 days ago = 11 points)
2 - Hello World ( 10 votes - 0 days ago = 10 points)
3 - Stackoverflow ( 12 votes - 2 days ago = 10 points)
我试试:
class DiffDays(Func):
function = 'DATEDIFF'
template = "%(function)s(%(expressions)s)"
class CastDate(Func):
function = 'DATE_FORMAT'
template = "%(function)s(%(expressions)s, '%%%%Y-%%%%m-%%%%d')"
list_posts = posts.extra(
select={'pub_date': "DATE_FORMAT(pub_date,'%%%%Y-%%%%m-%%%%d')"}
).annotate(
diff_days=DiffDays(
CastDate(today), F('pub_date')), output_field = DecimalField()
).annotate(
days_votes=ExpressionWrapper(
F('n_votes') - F('diff_days'), output_field=IntegerField())
).order_by('days_votes')
但我得到:'IntegerField' object has no attribute 'resolve_expression'
【问题讨论】:
标签: mysql django search filter annotate