Django框架之图书管理系统(二)
该篇文章介绍多对多的关系介绍
一、数据库设计
一个作者对应多个书籍
一个书籍对应多个作者
总结也就是多对多之间的关系
通过模型类创建多对多之间的关系表的时候,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框架之图书管理系统(二)的更多相关文章
- Django框架之图书管理系统(一)
图书管理系统共分为两篇博客进行讲解,该篇博客主要记录图书与出版社之间的关系(一对一),记录图书的增删查改操作 ============================================= ...
- Python-Flask框架之——图书管理系统 , 附详解源码和效果图 !
该图书管理系统要实现的功能: 1. 可以通过添加窗口添加书籍或作者, 如果要添加的作者和书籍已存在于书架上, 则给出相应的提示. 2. 如果要添加的作者存在, 而要添加的书籍书架上没有, 则将该书籍添 ...
- 新建Django项目示例--图书管理系统
知识点: Django 1. 安装 1. Django版本 1.11.xx 2. 安装方式 1. 命令行 --> Python环境(双版本,pip的使用) 2. PyCharm安装 2. 创建D ...
- Python-Flask框架之"图书管理系统"项目,附详解源代码及页面效果截图
该图书管理系统要实现的功能如下: 1. 可以通过添加窗口添加书籍或作者,如果要添加的作者和书籍已存在于书架上, 则给出相应的提示: 2. 如果要添加的作者存在,而要添加的书籍书架上没有,则将该书籍添加 ...
- Django框架(六):模型(二) 字段查询、查询集
1. 字段查询 通过模型类.objects属性可以调用如下函数,实现对模型类对应的数据表的查询. 函数名 功能 返回值 说明 get 返回表中满足条件的一条且只能有一条数据. 返回值是一个模型类对象. ...
- Django框架(九):视图(二) HttpRequest对象、HttpResponse对象
1. HttpRequest对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpR ...
- Python高级进阶(二)Python框架之Django写图书管理系统(LMS)
正式写项目准备前的工作 Django是一个Web框架,我们使用它就是因为它能够把前后端解耦合而且能够与数据库建立ORM,这样,一个Python开发工程师只需要干自己开发的事情就可以了,而在使用之前就我 ...
- 在Django中使用ORM创建图书管理系统
一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...
- [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二)
[入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二) Date 周六 10 一月 2015 By 钟谢伟 Category website develop ...
随机推荐
- react 实现圆环进度条
import React, { useState, useEffect } from "react" import { css } from "emotion" ...
- JAVAWEB应用模块(一)登录模块
java后台代码(MD5加密+token验证): import com.smart.ssai.admin.domain.User; import com.smart.ssai.admin.servic ...
- Java实现图形界面的三部曲及IDE中的窗口设计
设计和实现图形用户界面的工作主要有以下几点: • (1)创建组件(Component) • 创建组成界面的各种元素,如按钮.文本框等.• (2)指定布局(Layout) • 根据具体需要排列它们的位置 ...
- Codeforces Round #601 (Div. 2) B Fridge Lockers
//题目要求的是每一个点最少要有两条边连接,所以可以先构成一个环.然后再把剩余的最短的边连接起来 #include<iostream> #include<algorithm> ...
- 虫师自动化测试robot Framework 框架的学习
1.python关键字的定义 #coding=utf-8 def add(a,b): return a+b if __name__ == "__main__": c = add(4 ...
- 插件与App的跳转,及路由的关系
在SDK中 无法直接跳App 的界面,这个时候需要使用 路由,或者通过 NSClassFromString 的 presentViewController 来跳转. 直接贴代码: UIViewCont ...
- CentOS7利用docker安装MySQL5.7
CentOS7利用docker安装MySQL5.7 前提条件 centos7 且内核版本高于3.10, 可通过以下命令查看内核版本 uname -r 利用yum 安装docker 安装一些必要的系统工 ...
- SQL Server 2008创建数据库
1.数据.数据库.数据管理系统基本概念: 数据:人类有用信息的符号化表示. 数据库:按照数据结构来组织.存储和管理数据的一个仓库. 数据库管理系统(DBMS):可维护.存储并为应用系统提供数据的软件系 ...
- 解决 jmeter 压测Non HTTP response code: java.net.NoRouteToHostException/Non HTTP response message: Cannot assign requested address (Address not available)
针对centos : 先检查下tcp port range 在合理范围内: cat /proc/sys/net/ipv4/ip_local_port_range 1024 65535 上述为ce ...
- PHP+jPaginate插件制作无刷新分页实例
jPaginate是一款动感滚动分页插件,它的表现形式是像分页的按钮一样,有意思的是这些按钮却可以左右滚动,可以通过单击或 鼠标滑向点两侧的小箭头来控制按钮的左右滚动. 读取第一页数据: <di ...