小陆同学

python 中文名:蟒蛇,设计者:Guido van Rossum

导航

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列表

 

posted on 2020-05-27 21:27  小陆同学  阅读(555)  评论(0编辑  收藏  举报