gpt4 book ai didi

Django使用过滤条件将计数注释为计数

转载 作者:行者123 更新时间:2023-12-01 03:59:14 25 4
gpt4 key购买 nike

我需要在模型“Mymodel”中获取字段“title”的重复条目,其中计数大于 2。这样我就可以从 Mymodel 中删除所有重复项。

我正在尝试执行如下查询,但它抛出异常“AttributeError: 'bool' 对象没有属性 'lookup'”

movies = Mymodel.objects.values('title')\
.annotate(title_count=Count('title'), distint=True)\
.filter(title_count__gt=2)

等效的原始 sql 查询
SELECT count(title) as num_title, title from app_mymodel group by title having count(title) > 2;

我在这里发现了类似的问题, Filtering on the count with the Django ORM但它对我不起作用。

任何帮助都会很棒。

最佳答案

在没有 distinct 的情况下尝试类似的查询,因为我认为您不能将其传递给 annotate .

movies = Mymodel.objects.values('title')\
.annotate(title_count=Count('title'))\
.filter(title_count__gt=2)

关于Django使用过滤条件将计数注释为计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14597779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com