Django之數據表增刪改查
阿新 • • 發佈:2017-10-27
shortcuts add hid true 代碼 django app 的人 [0
Django數據增刪改查:
上課代碼
from django.shortcuts import render,HttpResponse # Create your views here. from app01 import models def addBook(request): # 一對多的添加 # 方式1 #models.Book.objects.create(title="python",price=100,publishDate="2017-12-12",publish_id=1)//這是規定傳值 # 方式2 # pubObj=models.Publish.objects.filter(name="人民出版社")[0]# models.Book.objects.create(title="python2",price=120,publishDate="2017-10-12",publish=pubObj) # 關聯的出版社對象這個比較靈活 # 多對多的添加 #book_obj=models.Book.objects.create(title="紅樓夢", price=100, publishDate="2017-12-12", publish_id=2) # alex_obj=models.Author.objects.filter(name="alex")[0] # egon_obj=models.Author.objects.filter(name="egon")[0]# egon_obj=models.Author.objects.filter(name="yuan")[0] # print("======",book_obj.authorlist) # [] # # authorList=models.Author.objects.all() # # # # 綁定多對多的關系 # # #book_obj.authorlist.add(alex_obj,egon_obj) # [alex_obj,egon_obj]#這個是添加可以列舉的作者 # book_obj.authorlist.add(*authorList) # [alex_obj,egon_obj]#這個是添加不可列舉的# 解除多對多的關系 # book_obj=models.Book.objects.filter(title="紅樓夢").first() # authorList=models.Author.objects.filter(id__lt=3) # print(book_obj.authorlist.remove(*authorList))#解除多對多關系的 # 清除關系方法 # book_obj = models.Book.objects.filter(title="紅樓夢").first() # book_obj.authorlist.clear()這是清除關系的 return HttpResponse("OK") def query(request): # 一對多的查詢 # 查詢linux這本書的出版社的地址? # linux_obj=models.Book.objects.filter(title="linux").first()#查出的是一個對象 # # print(linux_obj.title) # print(linux_obj.price) # print(linux_obj.publishDate) # # print(linux_obj.publish.name) # 與這本書對象關聯的出版社對象直接點就行了 # print(linux_obj.publish.addr) # 與這本書對象關聯的出版社對象 # 人民出版社出版過的書籍的名字 # publish_obj=models.Publish.objects.filter(name="人民出版社")[0] # print(publish_obj.bookList.all()) # 與這個出版社對象關聯的所有書籍對象 # 多對多的查詢 # 查詢追風箏的人的所有作者的姓名和年齡 # book_obj=models.Book.objects.filter(title="追風箏的人")[0] # print("=====",book_obj.authorlist.all() ) # 與這本書關聯的所有作者對象,集合對象 # authorlist=book_obj.authorlist.all()#這個是多個的就用.all() # print(authorlist.values("name","age")) # 查詢yuan出版過的所有書籍的名字和價格 #author_yuan=models.Author.objects.get(name="yuan") #print(author_yuan.book_set.all()) # 與這個作者關聯的所有書籍對象 #print(author_yuan.bookList.all().values("title","price"))
# 反向查找(一對多)
# publish_obj = models.Publish.objects.filter(name="人民出版社")[0]
# print(publish_obj.book_set.all().values("title","price"))#反向查找找到的是一個集合
#多對多反向查詢(多對多)
# author_obj=models.Author.objects.filter(name="frank").first()
#print(author_obj.book_set.all().values("title","price"))
# 一對一關系查詢 # 正向查詢 # 查詢手機號為456的作者的姓名 detail_obj=models.Author2Detail.objects.filter(tel="456").first() print(detail_obj.author.name)# 與tel="456"的Author2Detail關聯的作者對象 # 反向查詢 # 查詢景麗洋的手機號 author_obj=models.AuthorA.objects.filter(name="景麗洋").first() print(author_obj.author2detail.tel) # 789 print(author_obj.abc.tel) # 789 return HttpResponse("OK")
from django.db import models # Create your models here. class Book(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(max_length=32) publishDate = models.DateField() price = models.DecimalField(max_digits=5, decimal_places=2) wordNum=models.IntegerField(default=0)#增屬性的默認值可以自己設置 readNum=models.IntegerField(default=0)增屬性的默認值可以自己設置 publish=models.ForeignKey("Publish",related_name="bookList") authorlist=models.ManyToManyField("Author",related_name="bookList") # 多對多的關系,自動創建關系表 def __str__(self): return self.title class Publish(models.Model): name = models.CharField(max_length=32) addr = models.CharField(max_length=32) def __str__(self): return self.name class Author(models.Model): name=models.CharField(max_length=32) age=models.IntegerField() def __str__(self): return self.name class AuthorA(models.Model): name=models.CharField(max_length=32) age=models.IntegerField() class Author2Detail(models.Model): tel=models.IntegerField() addr=models.CharField(max_length=32) author=models.OneToOneField("AuthorA",related_name="abc") # class Book2Author(models.Model): # book=models.ForeignKey("Book") # author=models.ForeignKey("Author")
views
from django.shortcuts import render, HttpResponse from app01 import models # Create your views here. def addBook(request): # pass #建立一對一的關系 # models.Book.objects.create # 建立一對多關系 # 方法一 # models.Book.objects.create(title="python", publishDate="1998-10-27", price="233", publish_id=1) # 方法二 # publish_obj=models.Publish.objects.filter(name="人民出版社").first()#獲得是一個集合要變成一個對象 # models.Book.objects.create(title="python", publishDate="1998-10-27", price="233", publish=publish_obj) # 建立多對多關系 # author_obj1=models.Author.objects.filter(name="frank").first() # author_obj2=models.Author.objects.filter(name="egon").first() # book_obj=models.Book.objects.create(title="C", publishDate="1998-10-27", price="233", publish_id=2) # book_obj.authorlist.add(author_obj1,author_obj2) #建立多對多的關系最終版本 # book_obj = models.Book.objects.create(title="C", publishDate="1998-10-27", price="233", publish_id=2) # authorList=models.Author.objects.all() # book_obj.authorlist.add(*authorList) # 解除關系的 # book_obj=models.Book.objects.filter(title="C#").first() # author_obj=models.Author.objects.filter(name="frank").first() # print( book_obj.authorlist.remove(author_obj)) # #清空關系 # book_obj=models.Book.objects.filter(title="C#").first() # print( book_obj.authorlist.clear()) return HttpResponse("ok") def query(request): # 正向查詢 # 一對多查詢 # 查詢python的作者 # book_obj=models.Book.objects.filter(title="python")[0] # print(book_obj.publish.name) # print(book_obj.publish.addr) # 多對多查詢 # book_obj=models.Book.objects.filter(title="C").first() # print(book_obj.authorlist.all().values("name","age")) # 反向查找(一對多) # publish_obj = models.Publish.objects.filter(name="人民出版社")[0] # print(publish_obj.book_set.all().values("title","price"))#反向查找找到的是一個集合 #多對多反向查詢(多對多) author_obj=models.Author.objects.filter(name="frank").first() print(author_obj.book_set.all().values("title","price")) # 一對一關系查詢 # 正向查詢 # 查詢手機號為456的作者的姓名 detail_obj = models.Author2Detail.objects.filter(tel="456").first() print(detail_obj.author.name) # 與tel="456"的Author2Detail關聯的作者對象 # 反向查詢 # 查詢景麗洋的手機號 author_obj = models.AuthorA.objects.filter(name="景麗洋").first() print(author_obj.author2detail.tel) # 789#反向查詢不用加_set print(author_obj.abc.tel) # 789 return HttpResponse("查到了")數據增刪
models
from django.db import models # Create your models here. class Book(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) publish=models.ForeignKey("Publish") authorlist=models.ManyToManyField("Author") def __str__(self): return self.title class Publish(models.Model): name=models.CharField(max_length=32) addr=models.CharField(max_length=32) def __str__(self): return self.name class Author(models.Model): name=models.CharField(max_length=32) age=models.IntegerField() def __str__(self): return self.name class AuthorDetail(models.Model): telphone=models.IntegerField() addr=models.CharField(max_length=32) author=models.OneToOneField("Author") def __str__(self): return self.addrmodels代碼
Django之數據表增刪改查