按字段拆分查询集或获取多个查询集,而不仅仅是按字段排序 [英] Split queryset or get multiple querysets by field instead of just ordering by field

查看:84
本文介绍了按字段拆分查询集或获取多个查询集,而不仅仅是按字段排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以一种方式查询数据库,而不是仅按某个字段排序,而是为该字段的每个唯一值获取一个单独的QuerySet(或字典,列表等).希望以下示例会有所帮助:

I want to query a database in a way such that rather than just ordering by some field, I get a separate QuerySet (or dictionary, list, whatever) for each unique value of that field. Hopefully the below example will help:

假设像这样的模型

Class Person(models.Model):
   first_name = models.CharField()
   last_name = models.CharField

调用Person.objects.all().order_by('last_name')给我一个长的QuerySet.我希望每个唯一的last_name都有一个单独的列表.因此,每个具有last_name ="Smith"的人的列表,以及另一个具有last_name ="Nguyen"等人的列表.

Calling Person.objects.all().order_by('last_name') gives me a single long QuerySet. I want instead to have a separate list for each unique last_name. So one list for every Person with last_name="Smith" and another list for every Person with last_name="Nguyen" etc.

很显然,我无法提前知道数据库中将有哪些last_name,也无法知道有多少人共享一个通用的last_name.是否有任何快速,高效或自动的方式在django中做到这一点,还是我只需要在收回一个大查询集后自己处理数据?

Obviously I can't know ahead of time what last_names will be in the database, nor how many people will share a common last_name. Is there any fast, efficient, or automatic way to do this in django or do I just need to process the data myself after getting the one large queryset back?

推荐答案

您可以获得所有唯一的姓氏:

you can get all the unique lastnames:

from django.db.models import Count
...
last_names = Person.objects.values('last_name').annotate(Count('last_name')) # This will return all the unique last_names
values = dict( ((last_name['last_name'], Person.objects.all().filter(last_name = last_name['last_name'])) for last_name in last_names if last_name['last_name__count']) )
# This will create a dictionary where the keys are all the unique names and the values are querysect of all the values that have that lastname

丹尼尔·罗斯曼(Daniel-Roseman)是对的,它效率很低,所以这里有一个调整版本...

Daniel-Roseman is right it is pretty inefficient so heres a tweak version ...

from collections import defaultdict
values = defaultdict(list)
_ = map(lambda person: values[person.last_name].append(person), Person.objects.all())

请注意,_ = ...是这样,因此我们不会在终端上打印所有的None;)

note that _ = ... is so we don't print all the None on the terminal ;)

这篇关于按字段拆分查询集或获取多个查询集,而不仅仅是按字段排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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