1:
def annotate(self, *args, **kwargs)
# 用于实现聚合group by查询
2:
def distinct(self, *field_names) # 用于distinct去重 models.UserInfo.objects.values('nid').distinct() # select distinct nid from userinfo 注:只有在PostgreSQL中才能使用distinct进行去重
3:
models.UserInfo.objects.all()
models.UserInfo.objects.all().reverse()
这样取得数据一样
models.UserInfo.objects.all().order_by('-id')
models.UserInfo.objects.all().order_by('-id').reverse()
这样reverse才作用,上面是对id desc排序;而下面这对id正序排列
4:
from TB1
Entry.objects.extra(select={'new_id': "select col from sometable where othercol > 1"}
相当于SQL语句:
select
id,
(select col from sometable where othercol > 1) as new_id
from TB1
5:
def only(self, *fields): #仅取某个表中的数据 models.UserInfo.objects.only('username','id') 或 models.UserInfo.objects.filter(...).only('username','id')
only('username','id')===》[obj,obj,obj,]
6:
def defer(self, *fields): models.UserInfo.objects.defer('username','id') 或 models.UserInfo.objects.filter(...).defer('username','id') #映射中排除某列数据