day 64 Django 第五天 多表对多表的对应关系ORM
一、查
设置 Author表 在 views文件中
# 作者表 class Author(models.Model): id =models.AutoField(primary_key=True) name= models.CharField(max_length=32,null=False,unique=True) #告诉ORM 我这张表和book是多对多的对应关系,orm自动帮我生成第三张表 book= models.ManyToManyField(to='Book')
生成表在mysql中
D:\mysite>python3 manage.py makemigrations D:\mysite>python3 manage.py migrate
二、增
静态页面 Author_add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加作者信息</title> </head> <body> <h1>添加作者</h1> <form action="/Author_add/" method='post'> <p> 作者姓名:<input type="text" name='author_name'> </p> <p> <select multiple name="books"> {% for book in book_list %} <option value="{{ book.id }}">{{ book.title }}</option> {% endfor %} </select> </p> <p> <input type="submit" value="提交"> </p> </form> </body> </html>
from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^Publisher_list/',views.Publisher_list), url(r'^Publisher_add/',views.Publisher_add), url(r'^Publisher_del/',views.Publisher_del), url(r'^Publisher_edit/',views.Publisher_edit), url(r'^Author_list/',views.Author_list), url(r'^Author_add/',views.Author_add)
定义函数
def Author_add(request): if request.method =='POST': #获取前端表单作者姓名的变量: new_author_name =request.POST.get('author_name') # 获取前端表单多选框的书名 books = request.POST.getlist('books') #获取在数据库中添加新的作者名的对象 name为mysql表中的字段 new_author_obj = models.Author.objects.create(name =new_author_name) #获取相关书的信息 new_author_obj.book.set(books) print(11111) return redirect('/Author_list/') ret =models.Book.objects.all() print(ret) print(ret[0]) print(ret[0].title) return render(request,'author_add.html',{'book_list':ret})
提交页面
三、删除
urls
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^Publisher_list/',views.Publisher_list),
url(r'^Publisher_add/',views.Publisher_add),
url(r'^Publisher_del/',views.Publisher_del),
url(r'^Publisher_edit/',views.Publisher_edit),
url(r'^Author_list/',views.Author_list),
url(r'^Author_add/',views.Author_add),
url(r'^Author_del/',views.Author_del)
定义的函数在views里
def Author_del(request): del_id =request.GET.get('id') models.Author.objects.get(id =del_id).delete() return redirect('/Author_list/')
在静态网页中添加(修改Author_list.html文件)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Author_list</title> </head> <body> <table BORDER="1"> <thead> <tr> <td>#</td> <td>ID</td> <td>名字</td> <td>作品</td> <td>操作</td> </tr> </thead> <tbody> {% for author in author_list %} <tr> <td>{{ forloop.counter }}</td> <td>{{ author.id }}</td> <td>{{ author.name }}</td> <td> {% for book in author.book.all %} {{ book.tile }} {% endfor %} </td> <td> <a href="/Author_del/?id={{ author.id }}">删除</a> </td> </tr> {% endfor %} <a href="/Author_add/">添加新的作者</a> </tbody> </table> </body> </html>
四、编辑
author_edit.html 页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编辑页面</title> </head> <body> <h1>此页面为编辑页面</h1> <form action="/Author_edit/" method="post"> <p> <input type="hidden" name='author_id' value="{{ author.id }}"> </p> <p> <input type="text" name='author_name' value="{{ author.name }}"> </p> <p> <select multiple name="books"> {% for foo in book %} <option value="{{ foo.id }}">{{ foo.title }}</option> {% endfor %} </select> </p> <p> <input type="submit" value="提交"> </p> </form> </body> </html>
views里的函数
def Author_edit(request): if request.method=='POST': author_id =request.POST.get('author_id') author_name = request.POST.get('author_name') books_author =request.POST.getlist('books') author_obj= models.Author.objects.get(id =author_id) author_obj.name =author_name author_obj.book.set(books_author) author_obj.save() return redirect('/Author_list/') print(222) edit_id =request.GET.get('id') edit_id_obj =models.Author.objects.get(id =edit_id) print(1111) ret_obj =models.Book.objects.all() print(ret_obj) return render(request,'author_edit.html',{'book':ret_obj,'author':edit_id_obj})
老师写的views文件
# 编辑作者 def edit_author(request): # 如果编辑完提交数据过来 if request.method == "POST": # 拿到提交过来的编辑后的数据 edit_author_id = request.POST.get("author_id") new_author_name = request.POST.get("author_name") # 拿到编辑后作者关联的书籍信息 new_books = request.POST.getlist("books") # 根据ID找到当前编辑的作者对象 edit_author_obj = models.Author.objects.get(id=edit_author_id) # 更新作者的名字 edit_author_obj.name = new_author_name # 更新作者关联的书的对应关系 edit_author_obj.book.set(new_books) # 将修改提交到数据库 edit_author_obj.save() # 返回作者列表页,查看是否编辑成功 return redirect("/author_list/") # 从URL里面取要编辑的作者的id信息 edit_id = request.GET.get("id") # 找到要编辑的作者对象 edit_author_obj = models.Author.objects.get(id=edit_id) # 查询所有的书籍对象 ret = models.Book.objects.all() return render(request, "edit_author.html", {"book_list": ret, "author": edit_author_obj})