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()
相关推荐
Swizard4 小时前
别再让你的 Python 傻等了:三分钟带你通过 asyncio 实现性能起飞
python
悄悄敲敲敲4 小时前
MySQL表的约束
数据库·mysql
鼠爷ねずみ4 小时前
SpringCloud前后端整体开发流程-以及技术总结文章实时更新中
java·数据库·后端·spring·spring cloud
九皇叔叔5 小时前
MySQL 数据库 Read View 详解
数据库·mysql·mvcc·read view
Darkershadow5 小时前
python学习之串口通信
python·学习
Elastic 中国社区官方博客6 小时前
Elasticsearch:圣诞晚餐 BBQ - 图像识别
大数据·数据库·elasticsearch·搜索引擎·ai·全文检索
cui_win6 小时前
Prometheus实战教程 - Redis 监控
数据库·redis·prometheus
3824278276 小时前
python:输出JSON
前端·python·json
JIngJaneIL6 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
TG:@yunlaoda360 云老大7 小时前
华为云国际站代理商备份策略设置过程中遇到问题如何解决?
服务器·数据库·华为云