zoukankan      html  css  js  c++  java
  • django orm group_by Q查询

    from django.db.models import Max,Avg,F,Q,Min,Count,Sum
    this_year = datetime.datetime.now().year
    this_year_data = UserPrice.objects.filter(year=this_year).values('name','year').annotate(Sum('price'))
    # 查询表UserPrice的year字段等于今年的price的和
    type_data = PurchasingAsset.objects.filter(purchase_date__year=2018).values('cost_types').annotate(Sum('total_prices'))
    # 查询表PurchasingAsset里purchase_date__year字段为2018的按cost_types字段分类求total_prices的和

    Q查询

    from django.db.models import Q
    appCateList = [i.app_cate for i in App_Cate_User.objects.filter(Q(user=user,type='view') | Q(group__in=user.groups.all(),type='view'))]
    # 查询表App_Cate_User里user字段等于user,并且type=‘view'的或者group在user所在的多对多关系的组里,并且type=‘view'的数据,取出它们的app_cate字段组成list列表
  • 相关阅读:
    [ Python ] 递归函数
    [ Python ] 计算器
    [ Python ] 模块详解
    [ python ] 项目:haproxy配置文件增删改查
    [ Python ] 装饰器详解
    iOS设计
    Swift
    Swift
    iOS
    Swift
  • 原文地址:https://www.cnblogs.com/lutt/p/12976716.html
Copyright © 2011-2022 走看看