首页 > 其他分享 >Django 聚合查询

Django 聚合查询

时间:2024-09-15 11:53:43浏览次数:3  
标签:__ Book 聚合 models price 查询 books str Django

文章目录


一、聚合查询

使用聚合查询前要先从 django.db.models 引入 Avg、Max、Min、Count、Sum(首字母大写)
聚合查询返回值的数据类型是字典

聚合查询使用aggregate()对查询集执行聚合操作,它允许我们使用数据库提供的聚合函数(如 COUNT(), AVG(), SUM(), MAX(), MIN() 等)来对查询集中的数据进行汇总计算,aggregate() 方法返回一个字典,字典的键是你指定的聚合函数别名,值是聚合计算的结果

queryset.aggregate(聚合函数)

# 别名使用
queryset.aggregate(别名 = 聚合函数名("属性名称"))
# 例:
result = Book.objects.aggregate(average_price=Avg('price'), total_books=Count('id'))
# 结果示例: {'average_price': 100.25, 'total_books': 150}

二、使用步骤

1.准备工作

还是之前那个fa的项目目录,在views.py内引入

# 导入聚合函数
from django.db.models import Avg,Max,Min,Count,Sum

在models.py里面,定义一个Book模型

class Book(models.Model):
    title = models.CharField(max_length=255)  # 书名
    author = models.CharField(max_length=255)  # 作者
    price = models.DecimalField(max_digits=10, decimal_places=2)  # 价格
    rating = models.FloatField()  # 评分
    published_date = models.DateField()  # 出版日期
    pages = models.IntegerField()  # 页数

    def __str__(self):
        return self.title

在数据库生成book表,执行下列命令

python manage.py makemigrations
python manage.py migrate

这个时候我们就有一张book的表了,我们自己手动塞入一些数据(这里就不做新增了),然后在下一步实现聚合函数的使用
随意添加的数据
在这里插入图片描述

2.具体使用

定义一个方法去使用聚合函数,我们在views.py里面添加一个方法

def getBookSomeInfo(request):
    # 计算书的平均价格
    average_price = models.Book.objects.aggregate(avg_price = Avg('price'))
    # 如果不加别名结果为: {'price__avg': 100.25}

    # 获取书的最高价格
    max_price = models.Book.objects.aggregate(Max('price'))

    # 获取书的最低价格
    min_price = models.Book.objects.aggregate(min_price = Min('price'))

    # 统计书的总数量
    # book_count = models.Book.objects.aggregate(Count('id'))
    book_count = models.Book.objects.aggregate(book_count = Count('id'))
    # 其实也可以使用 len(models.Book.objects.all()) / models.Book.objects.count()

    # 计算所有书的总价格
    total_price = models.Book.objects.aggregate(total_price = Sum('price'))

    return HttpResponse(f"平均价格: {average_price}, 最高价格: {max_price}, 最低价格: {min_price}, 总数量: {book_count}, 总价格: {total_price}")

在路由urls.py里面添加

    path('getBookSomeInfo', views.getBookSomeInfo, name='getBookSomeInfo'),

访问链接http://127.0.0.1:8082/article/getBookSomeInfo
在这里插入图片描述

3.分组查询(annotate)

1.定义

annotate() 是 Django ORM 提供的一个方法,用于在查询集中为每个对象添加计算值。与 aggregate() 方法不同,annotate() 是逐个对象进行计算,而 aggregate() 是对整个查询集进行计算,并返回一个汇总结果

2.使用

使用前要先从 django.db.models 引入聚合函数,annotate() 方法接受一个或多个聚合函数作为参数,这些聚合函数会被应用到查询集中,并将结果作为额外字段添加到每个对象上

queryset.annotate(聚合函数)

3.具体案例

我们先修改一下刚刚定义的Book模型,同时增加一个作者User模型

class User(models.Model):
    name = models.CharField(max_length=255)  # 作者名称

    def __str__(self):
        return self.name


class Book(models.Model):
    title = models.CharField(max_length=255)  # 书名
    # author = models.CharField(max_length=255)  # 作者
    user = models.ForeignKey(User, related_name='books', null=True, on_delete=models.CASCADE)  # 作者
    price = models.DecimalField(max_digits=10, decimal_places=2)  # 价格
    rating = models.FloatField()  # 评分
    published_date = models.DateField()  # 出版日期
    pages = models.IntegerField()  # 页数

    def __str__(self):
        return self.title

执行命令生成数据表,user表和book表都先手动写入数据,不通过程序写入数据
views.py增加方法

def getBookFormUser(request):
    # 计算每个作者的书籍数量

    # 这里的 'books' 是 models.User 类中定义的外键名称,如果外键名称不是 'books' 则需要修改
    users_with_book_count = models.User.objects.annotate(book_count = Count('books'))

    users_with_book_count_str = ''
    # 输出每个作者的书籍数量
    for user in users_with_book_count:
        users_with_book_count_str += f"{user.name} has {user.book_count} books.\n"

    # 计算每个作者的书籍平均价格
    # books__price 代表 books 外键的 price 字段
    users_with_avg_price = models.User.objects.annotate(avg_price = Avg('books__price'))

    users_with_avg_price_str = ''
    # 输出每个作者的书籍平均价格
    for user in users_with_avg_price:
        users_with_avg_price_str += f"{user.name} has an average book price of {user.avg_price}.\n"

    # 计算每个作者的书籍总价格和最高评分
    users_with_totals = models.User.objects.annotate(total_price = Sum('books__price'), highest_rating = Max('books__rating'))

    users_with_totals_str = ''

    # 输出每个作者的书籍总价格和最高评分
    for user in users_with_totals:
        users_with_totals_str += f"{user.name} has a total book price of {user.total_price} and the highest rating is {user.highest_rating}.\n"

    # 返回带有换行符的 HTML 响应,确保编码为UTF-8
    return HttpResponse(
        f"每个作者的书籍数量: <br>{users_with_book_count_str}<br>"
        f"每个作者的书籍平均价格: <br>{users_with_avg_price_str}<br>"
        f"每个作者的书籍总价格和最高评分: <br>{users_with_totals_str}",
        content_type="text/html; charset=utf-8"
    )

增加路由

path('getBookFormUser', views.getBookFormUser, name='getBookFormUser'),

访问链接http://127.0.0.1:8000/article/getBookFormUser
在这里插入图片描述

4.F() 查询

1.定义

F() 表达式用于在数据库中直接引用字段的值,而不是将值从数据库取出后再进行计算。它允许你在数据库层面进行原子性的计算操作,从而避免出现竞争条件或数据不同步的问题,常用于以下场景:

对字段值进行加减、乘除等数学运算。
比较同一个模型中不同字段的值。
更新字段时直接使用该字段的当前值。

2.使用

要使用 F() 表达式,你需要从 django.db.models 中导入 F 类

from django.db.models import F
  1. 字段值的更新(如:增加浏览数)
  2. 先更新一下Book模型
class Book(models.Model):
    title = models.CharField(max_length=255)  # 书名
    # author = models.CharField(max_length=255)  # 作者
    user = models.ForeignKey(User, related_name='books', null=True, on_delete=models.CASCADE)  # 作者
    price = models.DecimalField(max_digits=10, decimal_places=2)  # 价格
    rating = models.FloatField()  # 评分
    published_date = models.DateField()  # 出版日期
    pages = models.IntegerField()  # 页数
    views = models.IntegerField(default=0)  # 浏览量

    def __str__(self):
        return self.title
  1. 定义方法和路由
def addViews(request):
    # 增加阅读量
    article_id = 1
    models.Book.objects.filter(id=article_id).update(views=F('views') + 1)
    return HttpResponse('Views added successfully')

path('addViews', views.addViews, name='addViews'),
  1. 访问链接http://127.0.0.1:8000/article/addViews
    在这里插入图片描述
  2. 一些其他场景
    定义方法和路由
def someOtherInfo(request):
    # 比较同一个模型的两个字段的值
    # 获取 price 大于 discount_price 的商品,这个查询会返回所有 price 大于 discount_price 的 Product 实例
    # 注意:F() 函数用于引用其他字段的值,不能用于直接比较两个字段的值,price和views都是字段名
    book_gt_discount = models.Book.objects.filter(price__gt=F('views'))
    book_gt_discount_str = ''
    for item in book_gt_discount:
        book_gt_discount_str += f"Book {item.id} has a price of {item.price} and views of {item.views}\n"
    
    # 多字段运算
    # 获取所有books的总价格和评分的乘积
    # 使用 F() 表达式在 annotate 中
    books = models.Book.objects.annotate(total_price=F('price') * F('views'))

    str_books = ''
    for item in books:
        str_books += f"Book {item.id} has total price {item.total_price}\n"
    
    # 查询时对字段进行运算
    # 获取book的价格大于其views加500的书籍
    high_books = models.Book.objects.filter(price__gt=F('views') + 500.00)
    high_books_str = ''
    for item in high_books:
        high_books_str += f"Book {item.id} has a price of {item.price}\n"

    return HttpResponse(
        f"book_gt_discount: <br>{book_gt_discount_str}<br>"
        f"str_books: <br>{str_books}<br>"
        f"high_books_str: <br>{high_books_str}",
        content_type="text/html; charset=utf-8"
    )

path('someOtherInfo', views.someOtherInfo, name='someOtherInfo'),

访问链接http://127.0.0.1:8000/article/someOtherInfo
在这里插入图片描述

5.Q() 查询

1.定义

Q() 对象来自 django.db.models,用于创建复杂的查询条件。你可以使用它来结合多个条件,执行与(AND)或或(OR)操作,甚至是非(NOT)操作,尤其是在需要执行“OR”操作或者需要多个条件组合时非常有用。Q() 对象使得构建复杂的查询变得更加灵活和强大,
使用前还是先导入

from django.db.models import Q

2.查询

定义方法和路由

def searchByq(request):
    # 按价格和阅读量查询书籍
    # 注意:Q() 函数用于构建复杂的查询条件,可以与其他条件组合使用
    # 这里的 Q() 函数与 price__gt 条件组合使用,表示价格大于 500.00
    # 与 views__gt 条件组合使用,表示阅读量大于 1
    books_and = models.Book.objects.filter(Q(price__gt=500.00) & Q(views__gt=1))
    books_and_str = ''
    for item in books_and:
        books_and_str += f"Book {item.id} has a price of {item.price} and views of {item.views}\n"
    
    # 按价格或阅读量查询书籍
    # 这里的 Q() 函数与 price__gt 条件组合使用,表示价格大于 500.00
    # 与 views__gt 条件组合使用,表示阅读量大于 1
    books_or = models.Book.objects.filter(Q(price__gt=500.00) | Q(views__gt=1))
    books_or_str = ''
    for item in books_or:
        books_or_str += f"Book {item.id} has a price of {item.price} and views of {item.views}\n"
    
    # 按价格范围查询书籍
    # 这里的 Q() 函数与 price__range 条件组合使用,表示价格在 500.00 到 1000.00 之间
    books_range = models.Book.objects.filter(Q(price__range=(500.00, 1000.00)))
    books_range_str = ''
    for item in books_range:
        books_range_str += f"Book {item.id} has a price of {item.price}\n"
    
    # not 查询 这里使用的 ~Q() 函数表示价格不大于 500.00
    books_not = models.Book.objects.filter(~Q(price__gt=500.00))
    books_not_str = ''
    for item in books_not:
        books_not_str += f"Book {item.id} has a price of {item.price}\n"
    return HttpResponse(
        f"books_and_str: <br>{books_and_str}<br>"
        f"books_or_str: <br>{books_or_str}<br>"
        f"books_range_str: <br>{books_range_str}"
        f"books_not_str: <br>{books_not_str}",
        content_type="text/html; charset=utf-8"
    )

    path('searchByq', views.searchByq, name='searchByq'),

访问链接http://127.0.0.1:8000/article/searchByq
在这里插入图片描述

标签:__,Book,聚合,models,price,查询,books,str,Django
From: https://blog.csdn.net/weixin_42695345/article/details/142257603

相关文章

  • 【开题报告】基于django+vue外卖订餐管理系统(论文+程序)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着互联网的飞速发展和生活节奏的加快,外卖订餐服务已成为现代都市生活中不可或缺的一部分。它不仅极大地便利了消费者的日常生活,也为餐饮......
  • jsp城市公交查询系统q208j
    jsp城市公交查询系统q208j本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表项目功能用户,站点信息,车次信息技术要求:   开发语言:JSP前端使用:HTML5,CSS,JSP动态网页技术后端使用SpringBoot,Spring技术主......
  • django实现分页的模块(导入即可用)
    `fromdjango.utils.safestringimportmark_safeimportcopyclassPagerPlay(object):definit(self,request,customer_list):self.customer_list=customer_listself.request_GET=copy.deepcopy(request.GET)self.request_GET._mutable=Trueself.page=request......
  • 【开题报告】基于django+vue基于Web的小型社区配送系统(论文+源码) 计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着电子商务的蓬勃发展和消费者对即时配送服务需求的日益增长,小型社区配送系统逐渐成为连接商家与居民的重要桥梁。传统社区配送往往面临......
  • python+django+mysql 教师培训反馈系统05141-计算机毕业设计项目选题推荐(赠源码)
       目   录摘  要Abstract第1章  前  言1.1 研究背景1.2 研究现状1.3 系统开发目标第2章  系统开发环境62.1HTTP协议62.2HTML网页技术62.3B/S结构62.4django脚本语言72.5MySQL数据库72.6Apache简介8第3章  需求分析......
  • MySQL 慢查询日志:解锁数据库性能优化的关键
    在MySQL数据库的使用过程中,性能优化是一个持续的挑战。而慢查询日志就是我们手中的一把利器,能够帮助我们发现和解决性能瓶颈。那么,MySQL中的慢查询日志究竟是什么呢?又该如何使用它来优化性能呢?让我们一起来深入了解。一、慢查询日志的定义与作用慢查询日志是MySQL数据库用来......
  • MySQL 慢查询日志:解锁数据库性能优化的关键
    在MySQL数据库的使用过程中,性能优化是一个持续的挑战。而慢查询日志就是我们手中的一把利器,能够帮助我们发现和解决性能瓶颈。那么,MySQL中的慢查询日志究竟是什么呢?又该如何使用它来优化性能呢?让我们一起来深入了解。一、慢查询日志的定义与作用慢查询日志是MySQL数......
  • mybatis plus多表查询的扩展
    mybatisplus提供了简单的CURD操作,但是有时我们的业务需要要求进行多表查询,这个时候,我们就需要加入多表查询的扩展了。 mybatis-plus-join,基于mybatis-plus的所有优点,然后还支持连表查询,还支持一对多,一对一的查询。mybatis-plus-join是mybatisplus的一个多表插件,上手简单,几分钟就......
  • 基于django+vue电脑DIY微信小程序演示录像22023【开题报告+程序+论文】-计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着科技的飞速发展,个人电脑(PC)已成为人们日常生活与工作中不可或缺的一部分。然而,在市场上琳琅满目的电脑配件和复杂多变的配置选项中,许多......
  • 基于django+vue电脑DIY微信小程序【开题报告+程序+论文】-计算机毕设
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着科技的飞速发展,个人电脑已成为日常生活与工作中不可或缺的工具。而电脑DIY(自己动手组装电脑)作为一种新兴的文化和趋势,不仅满足了用户......