简体   繁体   English

Django GROUP BY 字段值

[英]Django GROUP BY field value

Surprising that I could not find a way to make a group by query.令人惊讶的是,我找不到通过查询进行分组的方法。

I have a query set qs and I am trying to group by some_prop.val , considering qs is excluding entries where some_prop is None .我有一个查询集qs ,我试图按some_prop.val ,考虑到qs不包括some_propNone的条目。

Let's say the values are [1, 2, 3] , then I would be after a result like this:假设这些值为[1, 2, 3] ,那么我会得到这样的结果:

{1: entries1, 2: entries2, 3: entries3}

Does the Django ORM provide any feature to group results like this? Django ORM 是否提供任何功能来对这样的结果进行分组?

There is not a specific Django ORM way (as far as I know) but you can do the following to get a dictionary of entries grouped by the values of a field:没有特定的 Django ORM 方式(据我所知),但您可以执行以下操作来获取按字段值分组的条目字典:

  1. Use .values_list() with flat=True to get a list of the existent values in your database (if you don't know them beforehand).使用.values_list()flat=True来获取数据库中现有值的列表(如果您事先不知道它们)。 Also, use .distinct() to eliminate duplicate values as we do not care for those:此外,使用.distinct()消除重复值,因为我们不关心那些:

     value_list = MyModel.objects.values_list( 'interesting_field', flat=True ).distinct()
  2. Iterate through value_list and fill your dictionary:遍历value_list并填写您的字典:

     group_by_value = {} for value in value_list: group_by_value[value] = MyModel.objects.filter(interesting_field=value)

Now, the group_by_value dictionary contains as keys the distinct values in your interesting_field and as values the queryset objects, each containing the entries of MyModel with interesting_field=a value from value_list .现在, group_by_value字典包含作为键的不同值在您的interesting_field和作为值的查询集对象,每个都包含MyModel的条目,其interesting_field=a value from value_list




Leaving this here for comment legacy reasons.出于评论遗留原因,将其留在这里。

I have made a Q&A style example in, which simulates a COUNT ... GROUP BY SQL query.我做了一个问答风格的例子,它模拟了一个COUNT ... GROUP BY SQL 查询。

Essentially you need to utilize the .order_by for grouping and the .annotate() to count on the model's .values() .本质上,您需要利用.order_by进行分组,并利用.annotate()来依靠模型的.values()

Here is the above-mentioned example:这是上面提到的例子:

We can perform a COUNT ... GROUP BY SQL equivalent query on Django ORM, with the use of annotate() , values() , order_by() and the django.db.models 's Count methods:我们可以使用annotate()values()order_by()django.db.modelsCount方法在 Django ORM 上执行COUNT ... GROUP BY SQL 等效查询:

Let our model be:让我们的模型是:

 class Books(models.Model): title = models.CharField() author = models.CharField()

Let's assume that we want to count how many book objects per distinct author exist in our Book table:假设我们要计算每个不同作者在 Book 表中存在多少书籍对象:

 result = Books.objects.values('author') .order_by('author') .annotate(count=Count('author'))

Now result contains a queryset with two columns: author and count:现在结果包含一个带有两列的查询集:作者和计数:

 author | count ------------|------- OneAuthor | 5 OtherAuthor | 2 ... | ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM