And*_*oot 1 python django django-queryset django-postgresql
我有下一个查询集:
Article.objects.filter(finished=True, updated_at__range=[start, end]).extra(
select={'hour': 'extract(hour from updated_at)'}).values('hour').annotate(categorized=Count('id'))
Run Code Online (Sandbox Code Playgroud)
我得到错误:
“ ProgrammingError:列引用” updated_at“含糊不清LINE 1:SELECT(从updated_at提取(小时))AS” hour“,COUNT(” art ...“
我应该怎么做?
编辑:查询集的工作原理不按日期“ updated_at__range = [开始,结束]”进行过滤,但我需要该过滤器。
这与Django无关。您正在插入原始SQL(该extract子句),但无法限定其中来自updated_at的表。假设Article模型在名为“ myapp”的应用程序中,则可能是这样的:
select={'hour': 'extract(hour from myapp_article.updated_at)'})
Run Code Online (Sandbox Code Playgroud)
如果您是通过网络搜索来到这里的:column reference "created" is ambiguous在向过滤器添加prefetch_related和select_related语句时,我开始出现错误。
虽然最初我的查询.extra(where=['EXTRACT(hour from created) >19 OR EXTRACT(hour from created) <6'])有效,但在添加select_related / prefetch)后它停止工作。
解决方案是myappname_mymodelname.在字段名称之前添加。