该篇文章介绍多对多的关系介绍

一、数据库设计

一个作者对应多个书籍

一个书籍对应多个作者

总结也就是多对多之间的关系

通过模型类创建多对多之间的关系表的时候,Django框架通过ORM创建三个表,分别是作者表,书籍表,记录作者id对应书籍id的表

如下:

书籍表Books

作者表Author

作者书籍表author_book

二、代码部分

models.py代码部分:

class Books(models.Model):
"""
图书模型类
"""
id=models.AutoField(primary_key=True) # 表的id,AutoField是自动增长,相当于设置auto_increment
bookname=models.CharField(max_length=24) # 表的name,CharField是数据库中的varchar,max_length必须设置
publisher=models.ForeignKey(to="Publisher") # 表的外键,这是一对多关键的所在,to="Publisher"表示关联Publisher模型类的主键 class Author(models.Model):
id=models.AutoField(primary_key=True)
author=models.CharField(max_length=24,unique=True,null=False)
book=models.ManyToManyField(to="Books") # 多对多关系ManyToManyField

查:

urls.py

url(r'^author_books/$',show_author),

views.py

def show_author(request):
all_authors=Author.objects.all() # 查询所有的作者
# 通过后端获取id=1的作者名下的所有书籍
# Author.objects.get(id=1).book.all()
return render(request,"allauthor.html",{"authors_list":all_authors})

allauthor.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form>
<a href="/app04/addauthor/">添加作者</a>
<table border="1">
<tr>
<td>ID</td>
<td>作者</td>
<td>书籍</td>
<td>操作1</td>
<td>操作2</td>
</tr>
{% for authors in authors_list %}
<tr>
<td>{{ authors.id }}</td>
<td>{{ authors.author }}</td>
<td>
{% for b in authors.book.all %}
{% if forloop.last %}
{{ b.bookname }}
{% else %}
{{ b.bookname }},
{% endif %}
{% endfor %} </td>
<td><a href="/app04/deleteauthor/?id={{ authors.id }}">删除</a></td>
<td><a href="/app04/editorauthor/?id={{ authors.id }}">编辑</a> </td>
</tr>
{% endfor %}
</table>
</form>
</body>
</html>

总结:

多对多关系之间的设计,给表添加一个models.ManyToManyField(to="关联的表")

例如:表A和表B是多对多关系

表A:id,name,a_b=models.ManyToManyField(to="B")

表B:id,name,b_a=models.ManyToManyField(to="A")

例:获取表A中id=1数据下的所有数据(B中保存的)

A.objects.get(id=1).a_b.all()

====================================================================

增:

views.py

def add_author(request):
if request.method=="GET":
all_books=Books.objects.all()
return render(request,"addauthors.html",{"books_list":all_books})
if request.method=="POST":
get_author=request.POST.get("addauthor")
get_books=request.POST.getlist("addbooks")
create_author=Author.objects.create(author=get_author)
create_author.book.set(get_books)
return redirect("/app04/author_books/")

addauthors.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/app04/addauthor/" method="post">
<input type="text" name="addauthor" /><br/>
<select multiple name="addbooks">
{% for book in books_list %}
<option value="{{ book.id }}">{{ book.bookname }}</option>
{% endfor %} </select>
<input type="submit" valule="添加作者"/>
</form>
</body>
</html>

总结:

1.其中获取复选框获取多个id值:request.GET.getlist("参数名")或request.POST.getlist("参数名")

2.添加数据(全部借助上面的A,B表,其中假设A表是作者表,B表是书籍表)

添加作者名字为张三,著作有:书名1,书名2

add_author=A.objects.create(name="张三")

add_author.a_b.set(获取的多个书籍id)

=======================================================================

删:

views.py

def delete_author(request):
get_id=request.GET.get("id")
if get_id:
Author.objects.get(id=get_id).delete()
return redirect("/app04/author_books/")

总结:

删除作者id=1(肯定也要删除作者下的书籍)

A.objects.get(id=1).delete()

========================================================================

改:

views.py

def editor_author(request):
if request.method=="GET":
allbooks=Books.objects.all()
get_id=request.GET.get("id")
if get_id:
author=Author.objects.get(id=get_id) return render(request,"editorauthor.html",{"books_list":allbooks,"author":author})
if request.method=="POST":
get_id=request.POST.get("authorid")
get_author=request.POST.get("editorauthor")
get_booksid=request.POST.getlist("updatebooks")
update_author=Author.objects.get(id=get_id)
update_author.author=get_author
update_author.book.set(get_booksid)
update_author.save()
return redirect("/app04/author_books/")

editorauthor.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>编辑作者</title>
</head>
<body>
<form action="/app04/editorauthor/" method="post">
<input type="text" name="authorid" value="{{ author.id }}" style="display: none"/><br/>
<input type="text" name="editorauthor" value="{{ author.author }}"/><br/>
<select multiple name="updatebooks">
{% for book in books_list %}
{% if book in author.book.all %}
<option selected value="{{ book.id }}">
{{ book.bookname }}
</option>
{% else %}
<option value="{{ book.id }}">
{{ book.bookname }}
</option>
{% endif %}
{% endfor %}
</select><br/>
<input type="submit" value="更新"/>
</form>
</body>
</html>

总结:

update_author=Author.objects.get(id=get_id)
update_author.author=get_author
update_author.book.set(get_booksid)
update_author.save()

Django框架之图书管理系统(二)的更多相关文章

  1. Django框架之图书管理系统(一)

    图书管理系统共分为两篇博客进行讲解,该篇博客主要记录图书与出版社之间的关系(一对一),记录图书的增删查改操作 ============================================= ...

  2. Python-Flask框架之——图书管理系统 , 附详解源码和效果图 !

    该图书管理系统要实现的功能: 1. 可以通过添加窗口添加书籍或作者, 如果要添加的作者和书籍已存在于书架上, 则给出相应的提示. 2. 如果要添加的作者存在, 而要添加的书籍书架上没有, 则将该书籍添 ...

  3. 新建Django项目示例--图书管理系统

    知识点: Django 1. 安装 1. Django版本 1.11.xx 2. 安装方式 1. 命令行 --> Python环境(双版本,pip的使用) 2. PyCharm安装 2. 创建D ...

  4. Python-Flask框架之"图书管理系统"项目,附详解源代码及页面效果截图

    该图书管理系统要实现的功能如下: 1. 可以通过添加窗口添加书籍或作者,如果要添加的作者和书籍已存在于书架上, 则给出相应的提示: 2. 如果要添加的作者存在,而要添加的书籍书架上没有,则将该书籍添加 ...

  5. Django框架(六):模型(二) 字段查询、查询集

    1. 字段查询 通过模型类.objects属性可以调用如下函数,实现对模型类对应的数据表的查询. 函数名 功能 返回值 说明 get 返回表中满足条件的一条且只能有一条数据. 返回值是一个模型类对象. ...

  6. Django框架(九):视图(二) HttpRequest对象、HttpResponse对象

    1. HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpR ...

  7. Python高级进阶(二)Python框架之Django写图书管理系统(LMS)

    正式写项目准备前的工作 Django是一个Web框架,我们使用它就是因为它能够把前后端解耦合而且能够与数据库建立ORM,这样,一个Python开发工程师只需要干自己开发的事情就可以了,而在使用之前就我 ...

  8. 在Django中使用ORM创建图书管理系统

    一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...

  9. [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二)

    [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二) Date  周六 10 一月 2015 By 钟谢伟 Category website develop ...

随机推荐

  1. Introduction to Writing Functions in R

    目录 在R中编写函数 args(函数名) 创建一个函数的步骤 1.default args Passing arguments between functions Checking arguments ...

  2. Python 编辑注意点

    文本编辑 Python: 绝对不能用Word和Windows自带的记事本. (1)Word保存的不是纯文本文件 (2)记事本会自作聪明地在文件开始的地方加上几个特殊字符(UTF-8 BOM),结果会导 ...

  3. jQuery func

    1.  $(selector).each(function(index,element)); -----------index 选择器的index位置,element --当前的元素 2. _.eac ...

  4. Mike and Foam(位运算)

    English reading: bartender == barmaid:酒吧女招待 milliliter:毫升:千分之一毫升 foam:泡沫 a glass of beer with a good ...

  5. ubuntu19.04 配置远程连接ssh

    安装ssh-server sudo apt install openssh-server 参照:https://baijiahao.baidu.com/s?id=1631505486531979316 ...

  6. java基础之 开发环境配置

    一.Window 第一步:下载JDK 首先,我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/download ...

  7. python UI自动化生成BeautifulReport测试报告并保存截图

    前面已经写过利用BeautifulReport生成测试报告,那么接下来讲讲如何在测试报告里面保存截图 首先需要在测试用例中定义一个截图的方法: # 截图方法 """ os ...

  8. java i++与++i的区别

    i++是先赋值,然后再自+1:++i是先自+1,后赋值. 用代码表示就是: 若 a = i++; 则等价于 a=i;i=i+1; 而 a = ++i; 则等价于 i=i+1;a=i; 例子: int ...

  9. Go_json

    package main import ( "encoding/json" "fmt" ) // 结构体与json // 1.序列化: 把Go语言中的结构体变量 ...

  10. set_set_switching_activity

    set_switching_activity  -static   0.2   -toggle_rate   20 -period  1000 [all_inputs] 这时,翻转率设置的节点是输入, ...