Django之数据表增删改查
Posted 黎明NB
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django之数据表增删改查相关的知识,希望对你有一定的参考价值。
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.addr
以上是关于Django之数据表增删改查的主要内容,如果未能解决你的问题,请参考以下文章