django - Why is Model.objects.filter returning an empty query?
问题描述
I am reading an article on django about making queries over here. I have the following snippets.
>>> Entry.objects.first().pub_date
datetime.date(2021, 8, 18)
>>> Entry.objects.first().mod_date
datetime.date(2021, 8, 18)
But if I try the following I get an empty queryset.
Entry.objects.filter(pub_date__year=F('mod_date__year'))
Why is it not working. Is this a bug?
解决方案
It seems django handles __year
and the other __
date extractions differently with annotate or F expressions, compared to when you use it in a filter.
For example in Postgres, the query generated with filter(pub_date__year=F('mod_date__year'))
is:
WHERE EXTRACT(\'year\' FROM pub_date") = ("mod_date")
which would result with an error. But if used with an integer year like filter(pub_date__year=2021)
:
WHERE "pub_date" BETWEEN 2021-01-01 AND 2021-12-31'
USING EXTRACT:
To solve this, try to use Extract
to generate the correct filter:
from django.db.models.functions import Extract
Entry.objects.filter(pub_date__year=Extract('mod_date', 'year'))
Which will generate this query:
WHERE EXTRACT(\'year\' FROM "pub_date") = (EXTRACT(\'year\' FROM "mod_date"))'
推荐阅读
- netsuite - 显示网络套件中采购订单的“项目”子列表的所有字段
- ignite - 重启 docker 容器时,Ignite 缓存数据丢失
- javascript - 使用相等 (===) 或不相等 (!==) 运算符是否更高效?
- android - 在需要的设备中以编程方式启用自动启动权限
- laravel - Laravel 复杂的地方和地点
- elasticsearch - Elasticsearch - 使字段可聚合但不可搜索
- sql-server - Weka 3.8 - SQL Server 连接
- java - 如何在 Beanshell 中使用或转义 java 8 Lambda 表达式
- android - Android - SMS Retriever API - 计算应用程序的哈希字符串问题
- google-forms - 如何在谷歌表单中设置默认语言以从不同的浏览器进行调查