Skip to main content
 首页 » 编程设计

python之Django 查询集 : column reference is ambiguous with extra queryset

2025年05月04日24jillzhang

我有下一个查询集:

Article.objects.filter(finished=True, updated_at__range=[start, end]).extra( 
    select={'hour': 'extract(hour from updated_at)'}).values('hour').annotate(categorized=Count('id')) 

我得到了错误:

"ProgrammingError: column reference "updated_at" is ambiguous LINE 1: SELECT (extract(hour from updated_at)) AS "hour", COUNT("art..."

我该怎么办?

编辑: 查询集无需按日期“updated_at__range=[start, end]”过滤即可工作,但我需要该过滤器。

请您参考如下方法:

这与 Django 无关。您正在插入原始 SQL(extract 子句)但未能限定 updated_at 来自其中的表。假设 Article 模型在一个名为“myapp”的应用程序中,它将是这样的:

select={'hour': 'extract(hour from myapp_article.updated_at)'})