数据库全部读取,显示都没问题,怎么把重复的合并.,并在他们每个后面统计出数量。数据库里面就一列,里面有很多重复名称。
|
wellscFeb 23, 2018
1
set or sql distinct
|
|
huntzhanFeb 23, 2018
4
group by xxx count xxx
|
|
NaVientFeb 23, 2018
5
先 xxx.objects.filter().distinct().values()将结果取为字典
再 from collections import Counter 具体用法看看文档 |
|
wizardozFeb 23, 2018
6
楼主要的是不是这种?
>>> from django.db.models import Count >>> pubs = Publisher.objects.annotate(num_books=Count('book')) >>> pubs <QuerySet [<Publisher: BaloneyPress>, <Publisher: SalamiPress>, ...]> >>> pubs[0].num_books 73 https://docs.djangoproject.com/en/2.0/topics/db/aggregation/ |
|
xpresslinkFeb 23, 2018
7
假设有这样一个只有一字段的 Model
class MyModel(models.Model): □□□□item = models.CharField(max_length=100) □□□□def __str__(self):s □□□□□□□□return self.item □□□□class Meta: □□□□□□□□verbose_name = 'MM' >>> from temp.models import MyModel as MM MM.objects.values_list('item', flat=True) <QuerySet ['a', 'b', 'c', 'd', 'e', 'a', 'a', 'e', 'b', 'b', 'b', 'b', 'f', 'a', 'c', 'b']> >>> from django.db.models import Count >>> MM.objects.values_list('item', flat=True).annotate(Count('item')) <QuerySet ['c', 2, 'b', 6, 'a', 4, 'f', 1, 'e', 2, 'd', 1]> >>> from collections import Counter >>> Counter(MM.objects.values_list('item', flat=True)) Counter({'b': 6, 'a': 4, 'c': 2, 'e': 2, 'd': 1, 'f': 1}) |
|
xpresslinkFeb 23, 2018
8
>>> MM.objects.values_list('item').annotate(Count('item'))
<QuerySet [('c', 2), ('b', 6), ('a', 4), ('f', 1), ('e', 2), ('d', 1)]> |
|
9
|