八月22日,django知识点总结:
生活随笔
收集整理的這篇文章主要介紹了
八月22日,django知识点总结:
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
八月22日,知識點(diǎn)總結(jié):python manage.py makemigrations
python manage.py migrate
# 2、查詢作者“alex"參與編寫的所有書籍(兩種方式) class Author(models.Model):'''作者'''name = models.CharField(max_length=100)age = models.IntegerField()class BookType(models.Model):'''圖書類型'''caption = models.CharField(max_length=64)class Book(models.Model):'''圖書'''name = models.CharField(max_length=64)pages = models.IntegerField()price = models.DecimalField(max_digits=10, decimal_places=2)# 數(shù)字長度max_digits,有效位數(shù)decimal_placespubdate = models.DateField() ##出版事件authors = models.ManyToManyField(Author)book_type = models.ForeignKey(BookType)
? unique=true是指這個字段的值在這張表里不能重復(fù),所有記錄值都要唯一,就像主鍵那樣搜索nullable=false是這個字 ? 段在保存時必需有值,不能還是null值就調(diào)用save去保存入庫
一、增刪改查1、添加表內(nèi)容(三種方式)1.1 obj = models.UserType(列名=‘內(nèi)容’)obj.save()1.2 models.UserType.objects.create(列名=‘內(nèi)容’)1.3user_dict={'列名':‘內(nèi)容’,'列名','內(nèi)容’}models.UserType.objects.create(**user_dict)2、查詢數(shù)據(jù)2.1 request.POST.get('usernaem')#獲取單條數(shù)據(jù),不存在則報錯(不建議)2.2 ret =models.UserType.objects.all()#獲取全部print(type(ret),ret.query)for item in ret:#根據(jù)循環(huán)可以輸出表內(nèi)容print(item,item.nid,item.caption)#ret.query 返回一個原生SQL#ret,得到一個特殊的QuerySet對象2.3 ret =models.UserType.objects.all().values('nid')print(ret)#values('xxx') #返回一個列表中包含字典2.4 ret=models.UserType.objects.all().values_list('nid')print('ret')#返回一個列表中加元組2.5 model.UserType.objects.filter(name='name')# 獲取指定條件的數(shù)據(jù)3、更改數(shù)據(jù)庫內(nèi)容3.1 models.UserInfo.objects.filter(user='alex').update(email='123@qq.com')#將指定的條件更新到數(shù)據(jù)庫,可以使用字典**kwargs3.2 obj = models.UserInfo.objects.get(user='alex')obj = email='!!!!@!!!!!'obj.save()#修改單條數(shù)據(jù)4、刪除數(shù)據(jù)4.1models.UserInfo.objects.filter(name='alex').delete()按條件刪除二、雙下劃線操作1.連表操作1.1 ret =models.UserInfo.objects.all().values('user','user_type__caption')##user_type__caption __雙下劃線在這是執(zhí)行連表操作,輸出另外表內(nèi)容1.2 拿出類型是超級管理員的所有用戶ret = models.UserInfo.objects.filter(user_type__caption="管理員").values('user','user_type__caption')2.多對多(ManyToMany)注:創(chuàng)建多臺主機(jī),多個部門1.自動創(chuàng)建關(guān)系表1.1 h2g =models.ManyToManyField('host')#創(chuàng)建多對多關(guān)系1.1.1 將多臺機(jī)器分配給一個組add添加關(guān)系,remove 刪除表關(guān)系,delete刪除表關(guān)系和表 set 添加,刪除h = Host.objects.get(hid=1) ##只刪除表關(guān)系h.group_set.remove(*Group.objects.filter(gid__gt=1))h = Host.objects.get(hid=1) ##添加表關(guān)系h.group_set.add(*Group.objects.filter(gid__gt=1))h = Host.objects.get(hid=1)h.group_set.all().delete() #delete刪除表關(guān)系和表obj =Group.objects.get(gid=1)print(obj.gid,obj.name,obj.h2g.all()) #bj.h2g.all() 是關(guān)系表,沒創(chuàng)建關(guān)系輸出空列表q =models.objects.filter(hid__gt=3)obj.h2g.add(*q)#將多臺主機(jī)分到obj組2.2.2 將一臺主機(jī)分給多個組h =Host.objects.get(hid=1)obj =Group.objects.get(gid=1)obj.h2g.add(h) ##主機(jī)id等于1分配到第一組obj =Group.objects.get(gid=2) ##obj.h2g.add(h)###主機(jī)id等于1分配到第二組h =models.Host.objects.get(hid=1) ##找到第一臺主機(jī)h.group_set.add(*Group.objects.filter(gid__gt=2))#gid大于2的所有分組分配給H主機(jī)反向查找 表名__set ,查找什么表名就是什么h.models.Host.objects.get(hid=1)h.group_set.add(*models.Group.objects.filter(gid__gt=12))注:h = Host.objects.get(hid=1)h.group_set.add(1) ##可以直接寫數(shù)字添加h.group_set.add(Group.objects.get(gid=1))h.group_set.add(*[1,2,3]) ##可以用列表添加關(guān)系h.group_set.set(Group.objects.filter(gid__gt=18), clear=True)clear = True 清空在設(shè)置(添加)set 不清空添加注:附加update_or_create,get_or_create 這兩個是一樣的都是給group和關(guān)系表添加數(shù)據(jù) 前提是關(guān)系表里不存在這個數(shù)據(jù)列:# r = h.group_set.update_or_create(name='技術(shù)部')如果沒有技術(shù)部,就給group組中加上技術(shù)部,關(guān)系表中自動關(guān)聯(lián)一條數(shù)據(jù)# print(r)# r = h.group_set.get_or_create(name='人事部')# print(r)2.自己創(chuàng)建表關(guān)系HostToGroup.objects.create(Host_id_id=1,group_id_id=2,status=11)#添加內(nèi)容創(chuàng)建索引class HostToGroup(models.Model): # hgid = models.AutoField(primary_key=True) # host_id = models.ForeignKey('Host') # group_id = models.ForeignKey('Group') # status = models.IntegerField() # class Meta: # unique_together = [ ###定義唯一索引,里面的索引值就不可以重復(fù) # ('host_id', 'group_id'), # ]
?
?
實(shí)例
# 1、查詢所有圖書類型為“科學(xué)”的所有書籍的 名稱、價格、發(fā)布時間、圖書類型(兩種方式)# 2、查詢作者“alex"參與編寫的所有書籍(兩種方式) class Author(models.Model):'''作者'''name = models.CharField(max_length=100)age = models.IntegerField()class BookType(models.Model):'''圖書類型'''caption = models.CharField(max_length=64)class Book(models.Model):'''圖書'''name = models.CharField(max_length=64)pages = models.IntegerField()price = models.DecimalField(max_digits=10, decimal_places=2)# 數(shù)字長度max_digits,有效位數(shù)decimal_placespubdate = models.DateField() ##出版事件authors = models.ManyToManyField(Author)book_type = models.ForeignKey(BookType)
?
表中添加內(nèi)容
# models.Author.objects.create(name='alex',age=12)# models.Author.objects.create(name='sunqihu',age=15)# models.Author.objects.create(name='jay',age=16)## models.BookType.objects.create(caption='文學(xué)')# models.BookType.objects.create(caption='科學(xué)')# models.BookType.objects.create(caption='數(shù)學(xué)')## book_mode={"name":"一路向西",# "pages":300,# "price":50,# "pubdate":"2011-09-09",# "book_type_id":2## }# models.Book.objects.create(**book_mode)
添加關(guān)系
# h =models.Book.objects.get(id=1)# h.authors_set.add(models.Author.objects.filter(id=2))# h =models.Book.objects.all().get(id=1)# obj =models.Author.objects.all().filter(id=3)# print(type(obj))# # print(h,type(h))# obj.book_set.add(h)?
# 第一種方法# rat = models.Book.objects.filter(book_type__caption='科學(xué)').values(# 'name','price','pubdate','book_type__caption'# )# print(rat)# for i in rat:# print(i)# rem =models.Book.objects.filter(authors__name='sunqihu').values('name','authors__name')# print(rem)#第二種方法# objs =models.BookType.objects.get(caption="科學(xué)")# obj =objs.book_set.values('name','price','pubdate','book_type__caption')# print(obj)# obj = models.Author.objects.get(name='sunqihu')# obs =obj.book_set.values('name','authors__name')# print(obs)
?
轉(zhuǎn)載于:https://www.cnblogs.com/pythonxiaohu/p/5798471.html
總結(jié)
以上是生活随笔為你收集整理的八月22日,django知识点总结:的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [SQL基础教程]1-4 SQL 表的创
- 下一篇: win8.1远程连接Redis数据库