1 annotate 2 3 # models.Publish.objects.annotate(a=Avg('book__price')).values('a') 4 5 # models.Book.objects.values('publish_id','id').annotate(a=Avg('price')) {'pulish_id':1,'a':11.11}
F查询和Q查询
1 from django.db.models import F,Q 2 3 F 针对自己单表中字段的比较和处理 4 models.Book.objects.filter(good__gt=F('comment')*2) 5 models.Book.objects.all().update(price=F('price')+1) 6 7 Q & | 非~ 8 filter(Q(xx=11)|Q(ss=22)&Q(oo=33)) 9 filter(Q(Q(xx=11)|Q(ss=22))&Q(oo=33)) &优先级高 10 filter(Q(Q(xx=11)|Q(ss=22))&Q(oo=33),name='dd')
执行原生sql(了解)
1 models.Publish.objects.raw('原生sql') 2 3 from django.db import connection 4 cursor = connection.cursor() 5 cursor.excute(sql,[1,]) 6 cursor.fetchall()
展示sql的
1 models.Book.objects.filter(good__gt=F('comment')*2) 2 from django.db import connection 3 print(connection.queries)