Django学习路17_聚合函数(Avg平均值,Count数量,Max最大,Min最小,Sum求和)基本使用
使用方法: 类名.objects.aggregate(聚合函数名(‘表的列名‘)) 聚合函数名: Avg 平均值 Count数量 Max 最大 Min 最小 Sum 求和 示例: Student.objects.aggregate(Max(‘sage‘))
创建消费者数据表 Customer class Customer(models.Model): c_name = models.CharField(max_length = 16) # 消费者名字 c_cost = models.IntegerField(default = 10) # 消费的金额
产生迁移文件 python manage.py makemigrations 进行迁移 python manage.py migrate
设置数据
Max 示例
def getcostmax(request): cost_max = Customer.objects.aggregate(Max("c_cost")) print(cost_max) return HttpResponse("获取成功")
Min 示例
def getcostmin(request): cost_min = Customer.objects.aggregate(Min("c_cost")) print(cost_min) return HttpResponse("获取成功") 注: 不要忘记在 urls.py 中进行注册
Sum 示例
def getcostsum(request): cost_sum = Customer.objects.aggregate(Sum("c_cost")) print(cost_sum) return HttpResponse("获取成功")
Count 示例
def getcustomercount(request): customer_count = Customer.objects.aggregate(Count("c_name")) print(customer_count) return HttpResponse("获取成功") 注: 此时获取姓名即可,不用获取价格
Avg 示例
def getcostavg(request): cost_avg = Customer.objects.aggregate(Avg("c_cost")) print(cost_avg) return HttpResponse("获取成功")
导入的包 from django.db.models import Max, Min, Sum, Count, Avg from django.http import HttpResponse from django.shortcuts import render # Create your views here. from app5.models import User, Order, Grade, Customer
2020-05-11
相关推荐
ztyzly00 2020-06-12
zycchun 2020-03-15
wenjieyatou 2020-05-08
xuanlvhaoshao 2020-02-20
李玉志 2020-02-02
ztyzly00 2020-01-12
jiahaohappy 2019-12-29
herohope 2019-12-22
阿亮 2019-10-31
落地窗前梦残夜 2019-06-29
tuxlcsdn 2019-06-28
Depth 2019-06-27
世樹 2019-05-22
LinoHngJie 2019-04-16
programmerv 2019-06-20
异道 2019-01-07
mrandy 2019-02-10
Jinnan 2018-09-24