django 在查询中计算相同字段值的最有效方法 [英] django most efficient way to count same field values in a query

查看:28
本文介绍了django 在查询中计算相同字段值的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个有很多字段的模型,但我只关心一个 charfield.可以说 charfield 可以是任何东西,所以我不知道可能的值,但我知道这些值经常重叠.所以我可以有 20 个带有abc"的对象和 10 个带有xyz"的对象,或者我可以有 50 个带有def"的对象和 80 个带有stu"的对象,我有 40000 个没有重叠的对象,我真的不在乎.

如何有效地计数对象?我希望返回的内容类似于:

{'abc': 20, 'xyz':10, 'other': 10,000}

或类似的东西,无需进行大量 SQL 调用.

我不知道是否有人会看到这个,因为我编辑它有点晚了,但是......

我有这个模型:

<前>类动作(模型.模型):作者 = models.CharField(max_length=255)purl = models.CharField(max_length=255, null=True)

从答案来看,我已经这样做了:

<前>groups = Action.objects.filter(author='James').values('purl').annotate(count=Count('purl'))

但是……

这就是组:

<前>{"purl": "waka"},{"purl": "waka"},{"purl": "waka"},{"purl": "waka"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "lora"}

(我只是用虚拟值填充了 purl)

我想要的是

<前>{'waka':4,'mora':5,'lora':1}

希望有人会看到这个编辑...

编辑 2:

显然我的数据库(BigTable)不支持Django的聚合函数,这就是为什么我遇到了所有问题.

解决方案

您需要类似于count ... group by"的内容.您可以使用 django 的 ORM 的聚合功能来做到这一点:

from django.db.models import Count字段名 = 'myCharField'MyModel.objects.values(fieldname).order_by(字段名).annotate(the_count=Count(fieldname))

之前关于这个主题的问题:

Lets say if I have a model that has lots of fields, but I only care about a charfield. Lets say that charfield can be anything so I don't know the possible values, but I know that the values frequently overlap. So I could have 20 objects with "abc" and 10 objects with "xyz" or I could have 50 objects with "def" and 80 with "stu" and i have 40000 with no overlap which I really don't care about.

How do I count the objects efficiently? What I would like returned is something like:

{'abc': 20, 'xyz':10, 'other': 10,000}

or something like that, w/o making a ton of SQL calls.

EDIT:

I dont know if anyone will see this since I am editing it kind of late, but...

I have this model:

class Action(models.Model):
    author = models.CharField(max_length=255)
    purl = models.CharField(max_length=255, null=True)

and from the answers, I have done this:

groups = Action.objects.filter(author='James').values('purl').annotate(count=Count('purl'))

but...

this is what groups is:

{"purl": "waka"},{"purl": "waka"},{"purl": "waka"},{"purl": "waka"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "lora"}

(I just filled purl with dummy values)

what I want is

{'waka': 4, 'mora': 5, 'lora': 1}

Hopefully someone will see this edit...

EDIT 2:

Apparently my database (BigTable) does not support the aggregate functions of Django and this is why I have been having all the problems.

解决方案

You want something similar to "count ... group by". You can do this with the aggregation features of django's ORM:

from django.db.models import Count

fieldname = 'myCharField'
MyModel.objects.values(fieldname)
    .order_by(fieldname)
    .annotate(the_count=Count(fieldname))

Previous questions on this subject:

这篇关于django 在查询中计算相同字段值的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆