python django获取某个角色的某个数据和——例如:获取所有订单的应付金额总和

model关系如下:

python 复制代码
class Order(models.Model):
    '''订单'''
    product = models.ForeignKey('Product', on_delete=models.SET_NULL, blank=True, null=True, verbose_name="产品")

    no = models.CharField(max_length=50, blank=True, null=True, verbose_name='订单编号', db_index=True)
    total_money = models.BigIntegerField(default=0, blank=True, null=True, verbose_name='订单总金额(分)')
    settlement_money = models.BigIntegerField(default=0, blank=True, null=True, verbose_name='应付现金金额(分)')
    pay_money = models.BigIntegerField(default=0, blank=True, null=True, verbose_name='实付现金金额(分)')

    create_time = models.DateTimeField(auto_now_add=True, blank=True, null=True, verbose_name='创建时间')

获取指定时间段内所有订单的应付金额总和

方法一:使用aggregate()
python 复制代码
from django.db.models import Q
from django.db.models import Sum
from . import models

#方法一:
def get_count(request):
    lookups = Q()
    if request.GET.get('start_time'):
        lookups = lookups & Q(create_time__gte=request.GET.get('start_time')
    if request.GET.get('end_time')
        lookups = lookups & Q(create_time__lte=request.GET.get('end_time')

    total_order_money = models.Order.objects.filter(lookups).aggregate(total_settlement_money=Sum('settlement_money'))['total_settlement_money']
方法二:使用sum()
相关推荐
被放养的研究生7 分钟前
dir()与help()
python
我的xiaodoujiao20 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 20--PO(POM) 设计模式和用例撰写
python·学习·测试工具·设计模式·pytest
子不语18027 分钟前
深度学习(一)——基础知识:Python语言/解释器/环境/编辑器
python·学习
xiaoye370837 分钟前
达梦数据库连接配置yaml 文件配置
服务器·数据库·oracle
爱学习的爬虫1 小时前
Python实现Jenkins实现自动化执行Job
python·自动化·jenkins
一晌小贪欢1 小时前
Python爬虫第9课:验证码识别与自动化处理
爬虫·python·自动化·网络爬虫·python爬虫·python3
bin91531 小时前
AI工具赋能Python开发者:项目开发中的创意守护与效率革命
开发语言·人工智能·python·工具·ai工具
被放养的研究生1 小时前
Python常用的一些语句
开发语言·python
TDengine (老段)1 小时前
TDengine 数学函数 PI 用户手册
大数据·数据库·时序数据库·iot·tdengine·涛思数据
Run Freely9371 小时前
Python_封装案例
python