您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
from django.db import models
# Create your models here.
class Publisher(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64,null=False,unique=True)
def __str__(self):
return "publisher_name:{}".format(self.name)
class Book(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=128,null=False)
publisher = models.ForeignKey(to=Publisher) #外键关联
def __str__(self):
return "book_title:{}".format(self.title)
class Author(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=16,null=False)
book = models.ManyToManyField(to="Book") #跟BOOK多对多关系
def __str__(self):
return "author_name:{}".format(self.name)
重点:查询author表,通过多对多的关系author_book这个表的基础,查找出书的名称。
重点:通过book表的外键关联,查询出该书的出版社名称。
from ldap3 import Server, Connection, ALL, SUBTREE, ServerPool
from django.shortcuts import HttpResponse,render,redirect
from ormtest import models
import pymysql
from django.views import View
# Create your views here.
def author_list(request):
# author = models.Author.objects.get(id=1)
# print(author.book.all())
all_author = models.Author.objects.all()
return render(request,"author.html",{"author_list":all_author})
def book_list(request):
all_book = models.Book.objects.all()
return render(request,"book.html",{"book_list":all_book})
相应函数功能
def add_author(request):
if request.method == "POST":
new_author_name = request.POST.get("author_name")
#getlist方法,获取所有选择的书籍
books = request.POST.getlist("books")
#创建一个新的作者
new_author_obj = models.Author.objects.create(name=new_author_name)
#为该作者添加相应的关系书籍,为在author_book表中添加相应的记录
new_author_obj.book.set(books)
return HttpResponse("添加作者成功!")
all_book = models.Book.objects.all()
return render(request,"add_author.html",{"book_list":all_book})
return HttpResponse("OK")
相应的html代码
<body>
<form action="/ormtest/add_author/" 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>
展示效果:
def del_author(request):
#从URL值取到要删除的作者id
delete_id = request.GET.get("id")
print(delete_id)
#根据ID值取到要删除的对象,直接删除
#1、去作者和书的关联表,把对应的关联记录删除
#2、去作者表把作者删除
models.Author.objects.get(id=delete_id).delete()
return redirect("/ormtest/author/")
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("/ormtest/author/")
#html展示效果如添加页面同样
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。