如何使用django orm计数字段

发布于 2025-01-19 20:31:19 字数 285 浏览 2 评论 0原文

大家好,我试图分别计算活动和非活动状态,下面提到代码来计算所有,我需要分别计算活动和非活动状态,请帮助我。请参考示例数据

Type.objects.all().aggregate(Count('status'))

output=5 

“表格格式”

Hi Everyone i trying to count active and inactive status sepratally, below mention code to count all, i need to count active and inactive status sepratally,pls help me out. pls refere sample data

Type.objects.all().aggregate(Count('status'))

output=5 

table formet

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丘比特射中我 2025-01-26 20:31:19

看看 https://pythonguides.com/pythonguides.com/python-django-group-group-group-by/

Type.objects.values('status').annotate(
    status_count=Count('id')
).values(
    'status', 'status_count'
).order_by()

编辑:仅在活动状态中计数

Type.objects.filter(status='active').aggregate(
    active_count=Count('status')
).values('active_count')

Look at https://pythonguides.com/python-django-group-by/

Type.objects.values('status').annotate(
    status_count=Count('id')
).values(
    'status', 'status_count'
).order_by()

EDIT: count only in active status

Type.objects.filter(status='active').aggregate(
    active_count=Count('status')
).values('active_count')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文