1. 程式人生 > >Django之模型層,單,多表操作(二)

Django之模型層,單,多表操作(二)

一:單表操作之增,刪,改

  1.1:增

    方式一:

      book_obj=Book.objects.create(title="python葵花寶典",state=True,price=100,publish="蘋果出版社",pub_date="2012-12-12")

    方式二:

      book_obj=Book(title="python葵花寶典",state=True,price=100,publish="蘋果出版社",pub_date="2012-12-12")

      book_obj.save()

  1.2:改

      Book.objects.filter(title__startswith="py").update(price=120)

      此外,update()方法對於任何結果集(QuerySet)均有效,這意味著你可以同時更新多條記錄update()方法會返回一個整型數值,表示受影響的記錄條數。

  1.3:刪

  方式一:

    Entry.objects.filter(pub_date__year=2005).delete()

  方式二:

    b = Blog.objects.get(pk=1)

    # This will delete the Blog and all of its Entry objects.

    b.delete()

二:單表查詢

from
django.shortcuts import render,redirect,HttpResponse from app01.models import * # Create your views here. def single_table(request): #建立表記錄 # student1 =student(name='djp',age=22,birthday='2018-10-10',flag=True,salary=48888.88) # student1.save() # 查詢 與 QUERYset
# 1查詢全部 stu=student.objects.all() # 返回QuerySet[object,object2] print(stu) # 2:first ,last #返回object物件 stu=student.objects.first() #返回object物件 stu=student.objects.last() #3:filter 和get 和 exclude stu=student.objects.filter(name='yjp') #返回Queryset[object1,object2] print(stu) stu = student.objects.exclude(name='yjp') # 呼叫Queryset與條件不匹配 返回Queryset[object1,object2] print(stu) stu=student.objects.get(name='yjp') #返回一個object 用get 有且只能查詢到唯一的資料 print(stu) #4:exists 和 count 呼叫均為 Queryset stu=student.objects.all().count() print(stu) stu=student.objects.all().exists()#判斷查詢資料是否存在,返回boolean stu=student.objects.filter(name='xxx').exists() print(stu) #5:order_by 和 reverse 排序 返回Queryset stu =student.objects.all().order_by('salary').reverse() print(stu) #6:values 和 values_list 呼叫者queryset stu =student.objects.all().order_by('salary').reverse().values('name')# 返回queryset print(stu) stu =student.objects.all().values('name','salary')#返回Queryset字典 [{},{}]也可以進行 first last 操作 print(stu)#QuerySet [{'name': 'yjp', 'salary': Decimal('8888.88')}, {'name': 'xjp', 'salary': Decimal('48888.88')}] stu=student.objects.all().values_list('name','salary')#返回Queryset [(),()] print(stu)#[('yjp', Decimal('8888.88')), ('xjp', Decimal('48888.88'))]> #7:distinct()去除重複記錄 呼叫者quertset stu=student.objects.all().values('salary').distinct() print(stu) #8:基於雙下劃線的模糊查詢 # gt,lt 返回Queryset stu=student.objects.filter(salary__gt=9999) print(stu) # #注意 student.objects.==student.objects.all() stu =student.objects.all().filter(salary__lt=9999) print(stu) #in 存在 range 之間 contains 包含 icontains 不包含 year 年 startswith 以xx開頭 stu=student.objects.filter(salary__range=[1000,10000]) print(stu)#返回Que ryset stu=student.objects.filter(salary__in=[1000,10000]) print(stu)#返回Queryset stu=student.objects.filter(birthday__year='2018') print(stu) #返回Queryset #9;update 和 delect 呼叫者Queryset 返回 被修改記錄的條數 # stu=student.objects.filter(name='djp').update(salary=7777.77)#返貨 # print(stu) # stu =student.objects.filter(name='djp').delete() # print(stu) t =Teacher.objects.all() print(t) return HttpResponse('OK')
View Code

三:基於雙下劃線的模糊查詢

Book.objects.filter(price__in=[100,200,300])  # 存在
Book.objects.filter(price__gt=100)            # 大於
Book.objects.filter(price__lt=100)       # 小於 
Book.objects.filter(price__range=[100,200])  # 之間
Book.objects.filter(title__contains="python") # 是否是  返回boolean 
Book.objects.filter(title__icontains="python")# 是否不是  返回boolean
Book.objects.filter(title__startswith="py")   # 以xx開頭
Book.objects.filter(pub_date__year=2012)      # 日期的年是否是 2012