资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

6、django操作表多对多实战-创新互联

图书管理系统表设计结构
6、django操作表多对多实战

创新互联建站专注于华宁企业网站建设,成都响应式网站建设公司,成都商城网站开发。华宁网站建设公司,为华宁等地区提供建站服务。全流程定制网站,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务

author的id对应author_book的author_id
book的id对应author_book的book_id

#########orm工具设置
D:\mysite\polls\models.py
orm:对像关系映射,将Python语法自动转换成sql语句

from django.db import models

#书
class Book(models.Model):
    id = models.AutoField(primary_key=True)  # 自增的ID主键
    #创建一个varchar(64)的唯一的不为空的字段
    title = models.CharField(max_length=64, null=False, unique=True)
    #和出版社关联的外键字段
    publisher = models.ForeignKey(to="Publisher", on_delete=models.CASCADE)

#作者表
class Author(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=16, null=False, unique=True)
    #告诉ORM 我这张表和book表是多对多的关联关系,ORM自动帮我生成了第三张表
    book = models.ManyToManyField(to="Book")

    def __str__(self):
        return "".format(self.name)

会生成三张表
6、django操作表多对多实战
polls_book表
6、django操作表多对多实战
polls_author表
6、django操作表多对多实战
polls_author_books表
6、django操作表多对多实战

#########主url设置

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('polls/',include('polls.urls')),
    path('admin/', admin.site.urls),
]

#########应用url设置
D:\mysite\polls\urls.py

from django.urls import path

from . import views
app_name = 'polls'
urlpatterns = [

    #书相关的对应关系
    path('book_list/', views.book_list,name='book_list'),
    path('add_book/', views.add_book,name='add_book'),  # 添加书籍
    path('delete_book/', views.delete_book,name='delete_book'),  # 删除书籍
    path('edit_book/', views.edit_book,name='edit_book'),  # 编辑书籍

    # 作者相关的对应关系
    path('author_list/', views.author_list,name='author_list'),  # 展示作者
    path('add_author/', views.add_author,name='add_author'),  # 添加作者
    path('delete_author/', views.delete_author,name='delete_author'),  # 删除作者
    path('edit_author/', views.edit_author,name='edit_author'),  # 编辑作者

]

#########后端函数
D:\mysite\polls\views.py

from django.shortcuts import render,redirect,HttpResponse
from .models import Question,Publisher
from polls import models

#展示书的列表
def book_list(request):
    # 去数据库中查询所有的书籍
    all_book = models.Book.objects.all()
    #在HTML页面完成字符串替换(渲染数据)
    return render(request, "polls/book_list.html", {"all_book": all_book})

#添加书籍
def add_book(request):
    if request.method == "POST":
        new_title = request.POST.get("book_title")
        new_publisher_id = request.POST.get("publisher")
        #创建新书对象,自动提交
        models.Book.objects.create(title=new_title, publisher_id=new_publisher_id)
        #返回到书籍列表页
        return redirect("/polls/book_list/")

    #取到所有的出版社
    ret = models.Publisher.objects.all()
    return render(request, "polls/add_book.html", {"publisher_list": ret})

#删除书籍
def delete_book(request):
    #从URL里面获取要删除的书籍的id值
    delete_id = request.GET.get("id")  # 从URL里面取数据
    #去删除数据库中删除指定id的数据
    models.Book.objects.get(id=delete_id).delete()
    #返回书籍列表页面, 查看是否删除成功
    return redirect("/polls/book_list/")

#编辑书籍
def edit_book(request):
    if request.method == "POST":
        # 从提交的数据里面取,书名和书关联的出版社
        edit_id = request.POST.get("id")
        new_title = request.POST.get("book_title")
        new_publisher_id = request.POST.get("publisher")
        #更新
        edit_book_obj = models.Book.objects.get(id=edit_id)
        edit_book_obj.title = new_title  # 更新书名
        edit_book_obj.publisher_id = new_publisher_id  # 更新书籍关联的出版社
        #将修改提交到数据库
        edit_book_obj.save()
        #返回书籍列表页面,查看是否编辑成功
        return redirect("/polls/book_list/")

    #返回一个页面,让用户编辑书籍信息
    #取到编辑的书的id值
    edit_id = request.GET.get("id")
    #根据id去数据库中把具体的书籍对象拿到
    edit_book_obj = models.Book.objects.get(id=edit_id)
    ret = models.Publisher.objects.all()
    return render(
        request,
        "polls/edit_book.html",
        {"publisher_list": ret, "book_obj": edit_book_obj}
    )

#作者列表页
def author_list(request):
    # 查询所有的作者
    all_author = models.Author.objects.all()
    return render(request, "polls/author_list.html", {"author_list": all_author})

#添加作者
def add_author(request):
    if request.method == "POST":
        print("in post...")
        #取到提交的数据
        new_author_name = request.POST.get("author_name")
        #post提交的数据是多个值的时候一定会要用getlist,如多选的checkbox和多选的select
        books = request.POST.getlist("books")
        #创建作者
        new_author_obj = models.Author.objects.create(name=new_author_name)
        #把新作者和书籍建立对应关系,自动提交
        new_author_obj.book.set(books)
        #跳转到作者列表页面,查看是否添加成功!
        return redirect("/polls/author_list/")

    #查询所有的书籍
    ret = models.Book.objects.all()
    return render(request, "polls/add_author.html", {"book_list": ret})

#删除作者
def delete_author(request):
    # 从URL里面取到要删除的作者id
    delete_id = request.GET.get("id")
    #根据ID值取到要删除的作者对象,直接删除
    #1. 去作者表把作者删了
    #2. 去作者和书的关联表,把对应的关联记录删除了
    models.Author.objects.get(id=delete_id).delete()
    #返回作者列表页面
    return redirect("/polls/author_list/")

#编辑作者
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("/polls/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, "polls/edit_author.html", {"book_list": ret, "author": edit_author_obj})

#########静态html文件
#book列表页
D:\mysite\polls\templates\polls\book_list.htmll




    
    书籍列表



所有的书籍都在这里!

添加书籍 {% for i in all_book %} {% endfor %}
id title publisher 操作
{{ i.id }} {{ i.title }} {{ i.publisher.name }} 删除 编辑

##book添加页

D:\mysite\polls\templates\polls\add_book.html




    
    添加书籍



添加书籍

书名:

出版社:

#book编辑页

D:\mysite\polls\templates\polls\edit_book.html



    
    编辑书籍



编辑书籍

书名:

出版社:

#作者列表页
D:\mysite\polls\templates\polls\author_list.htm




    
    作者列表



添加新的作者

所有的作者

{% for author in author_list %} {% endfor %}
# ID 名字 作品 操作
{{ forloop.counter }} {{ author.id }} {{ author.name }} {% for book in author.book.all %} {{ book.title }}  {% endfor %} 删除 编辑

#作者添加页
D:\mysite\polls\templates\polls\add_author.html




    
    添加作者



添加作者

作者姓名:

作品:

#作者编辑页
D:\mysite\polls\templates\polls\edit_author.html




    
    编辑作者



编辑作者

作者姓名:

作品:

模板里author.book.all的含义
6、django操作表多对多实战

#web展示
作者列表页
6、django操作表多对多实战

作者添加页
6、django操作表多对多实战

作者编辑页
6、django操作表多对多实战

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:6、django操作表多对多实战-创新互联
分享链接:http://cdkjz.cn/article/jsioe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220