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

一、数据库设计

一个作者对应多个书籍

一个书籍对应多个作者

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

通过模型类创建多对多之间的关系表的时候,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()

最新文章

  1. Mac下搭建php开发环境【转】
  2. GitHub: Windows 下的简单使用
  3. ASP.NET MVC 如何解决“上下文的模型已在数据库创建后发生更改”问题
  4. Delphi的TThread中的FreeOnTerminate成员
  5. MVC文件夹
  6. JavaScript原生对象属性和方法详解——Array对象
  7. python批量导出导入MySQL用户的方法
  8. UVa 10474 Where is the Marble
  9. Ubuntu14.04下如何开启Mysql远程访问
  10. C++ 64位操作系统调用 RegOpenKey() 读取注册表,返回 2, ERROR_FILE_NOT_FOUND
  11. python第十八天 多态 和 私有
  12. vim设置行号
  13. uva-270-排序
  14. Java设计模式之【单例模式】
  15. Android--PullToRefreshListView的onRefreshComplete()不起作用的问题
  16. DML-插入
  17. 24 - 面向对象基础-多继承-super-mro-Mixin
  18. oracle 命令 登录
  19. 玩转DOM遍历——用NodeIterator实现getElementById,getElementsByTagName方法
  20. BZOJ4236 JOIOJI 【map】

热门文章

  1. resample matlab实现
  2. js获取当前页面的url网址信息
  3. 计算a除b的第一位小数 in C++.
  4. 2019杭电多校第五场 discrete logarithm problem
  5. Electron – 项目报错整理(打包~1): WARNING: Make sure that .NET Framework 4.5 or later and Powershell 3 or later are installed, otherwise extracting the Electron zip file will hang.
  6. c# 技巧
  7. js splice()
  8. 7.log4j
  9. c++中sort函数调用报错Expression : invalid operator &lt;的内部原理 及解决办法
  10. NOIP2012 疫情控制 题解(LuoguP1084)