栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Django查询集中使用条件注释计数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Django查询集中使用条件注释计数

对于Django> = 1.8
使用条件聚合:

from django.db.models import Count, Case, When, IntegerFieldArticle.objects.annotate(    numviews=Count(Case(        When(readership__what_time__lt=treshold, then=1),        output_field=IntegerField(),    )))

说明: 通过你的文章进行的常规查询将使用

numviews
字段注释。该字段将被构造为CASE / WHEN表达式(由Count包裹),对于符合
NULL
读者身份的条件和不符合条件的读者,将返回1 。计数将忽略空值,仅计数值。

对于近期未查看过的文章,你将得到零,并且可以使用该

numviews
字段进行排序和过滤。

PostgreSQL的查询如下:

SELECt    "app_article"."id",    "app_article"."author",    "app_article"."published",    COUNT(        CASE WHEN "app_readership"."what_time" < 2015-11-18 11:04:00.000000+01:00 THEN 1        ELSE NULL END    ) as "numviews"FROM "app_article" LEFT OUTER JOIN "app_readership"    ON ("app_article"."id" = "app_readership"."which_article_id")GROUP BY "app_article"."id", "app_article"."author", "app_article"."published"

如果我们只想跟踪唯一查询,则可以在中添加区分

Count
,并使
When
子句返回值,我们想区分。

from django.db.models import Count, Case, When, CharField, FArticle.objects.annotate(    numviews=Count(Case(        When(readership__what_time__lt=treshold, then=F('readership__reader')), # it can be also `readership__reader_id`, it doesn't matter        output_field=CharField(),    ), distinct=True))

这将产生:

SELECt    "app_article"."id",    "app_article"."author",    "app_article"."published",    COUNT(        DISTINCT CASE WHEN "app_readership"."what_time" < 2015-11-18 11:04:00.000000+01:00 THEN "app_readership"."reader_id"        ELSE NULL END    ) as "numviews"FROM "app_article" LEFT OUTER JOIN "app_readership"    ON ("app_article"."id" = "app_readership"."which_article_id")GROUP BY "app_article"."id", "app_article"."author", "app_article"."published"对于django <1.8和PostgreSQL

你可以仅

raw
用于执行由django的较新版本创建的SQL语句。显然,没有一种简单而优化的方法可以在不使用数据的情况下查询该数据
raw
(即使
extra
注入必填
JOIN
子句存在一些问题)。

Articles.objects.raw('SELECt'    '    "app_article"."id",'    '    "app_article"."author",'    '    "app_article"."published",'    '    COUNT('    '        DISTINCT CASE WHEN "app_readership"."what_time" < 2015-11-18 11:04:00.000000+01:00 THEN "app_readership"."reader_id"'    '        ELSE NULL END'    '    ) as "numviews"'    'FROM "app_article" LEFT OUTER JOIN "app_readership"'    '    ON ("app_article"."id" = "app_readership"."which_article_id")'    'GROUP BY "app_article"."id", "app_article"."author", "app_article"."published"')


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/420422.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号