Model 操作表
一、基本操作
# 增models.Tb1.objects.create(c1='xx', c2='oo') #增加一條數(shù)據(jù),可以接受字典類型數(shù)據(jù) **kwargsobj = models.Tb1(c1='xx', c2='oo')obj.save() dic = {'c1':'xx','c2':'oo'} models.Tb1.objects.create(**dic) #Form的產(chǎn)出結(jié)果是一個(gè)字典,可以根據(jù)這個(gè)Form的字典和**直接在數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)# 查models.Tb1.objects.get(id=123) # 獲取單條數(shù)據(jù),不存在則報(bào)錯(cuò)(不建議)models.Tb1.objects.all() # 獲取全部 .first() 取第一條數(shù)據(jù)models.Tb1.objects.filter(name='seven') # 獲取指定條件的數(shù)據(jù) 也可以用**的方式傳參數(shù)# 刪models.Tb1.objects.filter(name='seven').delete() # 刪除指定條件的數(shù)據(jù)# 改models.Tb1.objects.filter(name='seven').update(gender='0') # 將指定條件的數(shù)據(jù)更新,均支持 **kwargsobj = models.Tb1.objects.get(id=1)obj.c1 = '111'obj.save() # 修改單條數(shù)據(jù)
細(xì)看從數(shù)據(jù)庫取出的數(shù)據(jù)類型 :
w = models.Simp.objects.all() print w, type(w)[<Simp: chenc>, <Simp: zan>, <Simp: zhangsan>] <class 'django.db.models.query.QuerySet'>
可以看到,從數(shù)據(jù)庫取出個(gè)數(shù)據(jù)看起來像包含對象的列表。而實(shí)際上整個(gè)數(shù)據(jù)為django中的特殊類型QuerySet。
.all()是取得所有列的數(shù)據(jù),可以加.values()取出某一列,每一項(xiàng)的值為一個(gè)字典:
w = models.Simp.objects.all().values('username')print w, type(w)[{'username': u'chenc'}, {'username': u'zan'}, {'username': u'zhangsan'}] <class 'django.db.models.query.QuerySet'>
.values_list(),獲取到的值為一個(gè)元組
w = models.Simp.objects.all().values_list('username')print w, type(w)[(u'chenc',), (u'zan',), (u'zhangsan',)] <class 'django.db.models.query.QuerySet'>
.values_list()也可以添加多個(gè)參數(shù):(可以配合Form在前端生成動(dòng)態(tài)的select)
w = models.Simp.objects.all().values_list('id', 'username')print w, type(w)[(1, u'chenc'), (2, u'zan'), (3, u'zhangsan')] <class 'django.db.models.query.QuerySet'>
query可以查看執(zhí)行的sql語句:
b = models.Simp.objects.all()print b.querySELECT "app01_simp"."id", "app01_simp"."username", "app01_simp"."password" FROM "app01_simp"
二、進(jìn)階操作
利用雙下劃線將字段和對應(yīng)的操作連接起來
# 獲取個(gè)數(shù) # # models.Tb1.objects.filter(name='seven').count() # 大于,小于 # # models.Tb1.objects.filter(id__gt=1) # 獲取id大于1的值 # models.Tb1.objects.filter(id__lt=10) # 獲取id小于10的值 # models.Tb1.objects.filter(id__lt=10, id__gt=1) # 獲取id大于1 且 小于10的值 # in # # models.Tb1.objects.filter(id__in=[11, 22, 33]) # 獲取id等于11、22、33的數(shù)據(jù) # models.Tb1.objects.exclude(id__in=[11, 22, 33]) # not in # contains # # models.Tb1.objects.filter(name__contains="ven") # models.Tb1.objects.filter(name__icontains="ven") # icontains大小寫不敏感 # models.Tb1.objects.exclude(name__icontains="ven") # range # # models.Tb1.objects.filter(id__range=[1, 2]) # 范圍bettwen and # 其他類似 # # startswith,istartswith, endswith, iendswith, # order by # # models.Tb1.objects.filter(name='seven').order_by('id') # asc 從小到大 # models.Tb1.objects.filter(name='seven').order_by('-id') # desc 從大到小 # limit 、offset # # models.Tb1.objects.all()[10:20] # group by from django.db.models import Count, Min, Max, Sum # models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num')) # SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"
新聞熱點(diǎn)
疑難解答
圖片精選