【发布时间】:2018-04-06 15:11:42
【问题描述】:
我想根据不区分大小写的字段值获取所有重复项。
基本上就是重写这个SQL查询
SELECT count(*), lower(name)
FROM manufacturer
GROUP BY lower(name)
HAVING count(*) > 1;
使用 Django ORM。我希望这样的事情能解决问题
from django.db.models import Count
from django.db.models.functions import Lower
from myapp.models import Manufacturer
qs = Manufacturer.objects.annotate(
name_lower=Lower('name'),
cnt=Count('name_lower')
).filter('cnt__gt'=1)
但它当然没有用。
知道怎么做吗?
【问题讨论】:
-
Manufacturer.objects.values(name).annotate(cnt=Count('name_lower')).filter('cnt__gt'=1)
-
@SergAnuke that will not work as
name_lower不是实际列,而是来自注释 -
.annotate(name_lower=Lower('name')) .values('name_lower')
标签: python django duplicates django-annotate