您好,登錄后才能下訂單哦!
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) #外鍵關聯(lián)
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表的外鍵關聯(lián),查詢出該書的出版社名稱。
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})
相應函數(shù)功能
def add_author(request):
if request.method == "POST":
new_author_name = request.POST.get("author_name")
#getlist方法,獲取所有選擇的書籍
books = request.POST.getlist("books")
#創(chuàng)建一個新的作者
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)
#根據(jù)ID值取到要刪除的對象,直接刪除
#1、去作者和書的關聯(lián)表,把對應的關聯(lián)記錄刪除
#2、去作者表把作者刪除
models.Author.objects.get(id=delete_id).delete()
return redirect("/ormtest/author/")
def edit_author(request):
if request.method =="POST":
#拿到提交過來的編輯后的數(shù)據(jù)
edit_author_id = request.POST.get("author_id")
new_author_name = request.POST.get("author_name")
#拿到編輯后作者關聯(lián)的書籍信息
new_books = request.POST.getlist("books")
#根據(jù)ID找到當前編輯的作者對象
edit_author_obj = models.Author.objects.get(id=edit_author_id)
#更新作者名字
edit_author_obj.name = new_author_name
#更新作者關聯(lián)的書的對應關系
edit_author_obj.book.set(new_books)
#將修改提交到數(shù)據(jù)庫
edit_author_obj.save()
#返回作者列表頁,查看是否編輯成功
return redirect("/ormtest/author/")
#html展示效果如添加頁面同樣
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。