• aggregate和annotate方法使用


    1. 介绍

    aggregate:聚合

    annotate:分组

    Django的aggregate和annotate方法属于高级查询方法,主要用于组合查询。

    当我们需要对查询集(queryset)的某些字段,进行计算,或进行先分组,再计算或排序, 我们就需要使用aggregate和annotate方法了。

    一般搭配聚合函数进行使用。

    model.py

    1. from django.db import models
    2. class Tag(models.Model):
    3. name = models.CharField(verbose_name='标签名称', max_length=225)
    4. def __str__(self):
    5. return self.name
    6. class Article(models.Model):
    7. title = models.CharField(verbose_name='标题', max_length=225)
    8. content = models.CharField(verbose_name='内容', max_length=225)
    9. # 外键
    10. tag = models.ManyToManyField(verbose_name='标签', to='Tag', related_name='tag_article')
    11. price = models.DecimalField(max_digits=4, decimal_places=2)
    12. def __str__(self):
    13. return self.title

    2. aggregate使用

    2.1 基础使用

    1. from django.db.models import F, Max, Min, Avg
    2. from blog import models
    3. # 计算所有文章价格的平均值 # {'price__avg': Decimal('50.000000')}
    4. article_queryset = models.Article.objects.all().aggregate(Avg('price'))

    2.2 其他使用

    1. # 计算所有文章价格的总价钱 ,并其名称叫sum_price # {'sum_price': Decimal('250.00')}
    2. article_queryset = models.Article.objects.aggregate(sum_price=Sum('price'))
    3. # 文章最大价格 # {'max_price': Decimal('50.00')}
    4. article_queryset = models.Article.objects.aggregate(max_price=Max('price'))
    5. # # 同时获取文章价格的平均值, 价格的总价钱, 文章最大价格 # {'price_avg': Decimal('50.000000'), 'price_sum': Decimal('250.00'), 'price_max': Decimal('50.00')}
    6. article_queryset = models.Article.objects.aggregate(price_avg=Avg('price'), price_sum=Sum('price'),
    7. price_max=Max('price'))
    8. # 根据标签反查文章的最大价格 # {'max_price': Decimal('50.00')}
    9. article_queryset = models.Tag.objects.filter(id=1).first().tag_article.all().aggregate(max_price=Max('price'))
    10. print(article_queryset)

     3. annotate

    3.1 基础使用

    1. # 基础使用(配合values使用)
    2. # 按标签名称分组,再统计每组文章的最大价格
    3. #
    4. article_queryset = models.Tag.objects.values('name').annotate(Max('tag_article__price'))
    5. # 按文章标题进行分组,统计每个文章下面有多少标签
    6. #
    7. # {'title': 'fas', 'tag__count': 1}, {'title': '凤梨的108种吃法', 'tag__count': 1}]>
    8. article_queryset = models.Article.objects.values('title').annotate(Count('tag'))
    9. # 按文章标题进行分组,统计每个文章下面有多少标签,并定义返回的数据名称
    10. article_queryset = models.Article.objects.values('title').annotate(tag_count=Count('tag'))
    11. # 按标签名称分组,再统计每组文章数量
    12. article_queryset = models.Tag.objects.values('name').annotate(Count('tag_article'))
    13. # Annotate方法与Filter方法联用
    14. # 先按标签名称分组,再统计每组文章数量, 然后筛选出文章数量大于1的数据
    15. #
    16. article_queryset = models.Tag.objects.values('name').annotate(article_count=Count('tag_article')).filter(
    17. article_count__gt=1)
    18. # Annotate方法与order_by
    19. # 先按标签名称分组,再统计每组文章数量, 然后筛选出文章数量大于1的数据,最后以id倒叙排列
    20. article_queryset = models.Tag.objects.values('name').annotate(article_count=Count('tag_article')).filter(
    21. article_count__gt=1).order_by('-id')
    22. # 分组聚合联合使用
    23. # 先按标签名称分组,再统计每组文章数量, 然后筛选出文章数量大于1的数据,最后以id倒叙排列,取出标签id最大值
    24. article_queryset = models.Tag.objects.values('name').annotate(article_count=Count('tag_article')).filter(
    25. article_count__gt=1).order_by('-id').aggregate(Max('id'))

    本文借鉴

    (2条消息) aggregate和annotate方法使用详解与示例_weixin_33970449的博客-CSDN博客

    Django基础(24): aggregate和annotate方法使用详解与示例 (qq.com)

  • 相关阅读:
    使用Bochs调试操作系统代码
    「记录」MMDetection入门篇
    人工神经网络用什么软件,神经网络能用来做什么
    【JAVA-Day15】Java 的 do-while 循环语句
    SQL Server函数
    (Bezier)贝塞尔曲在路径规划的运用
    期望和方差
    JVM内存管理面试常见问题全解
    kotlin 之几个常见的内联函数(一)
    操作系统--赵XX
  • 原文地址:https://blog.csdn.net/qq_52385631/article/details/126708420