站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到城東網(wǎng)站設(shè)計(jì)與城東網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請(qǐng)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋城東地區(qū)。
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) #外鍵關(guān)聯(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多對(duì)多關(guān)系
def __str__(self):
return "author_name:{}".format(self.name)
重點(diǎn):查詢author表,通過(guò)多對(duì)多的關(guān)系author_book這個(gè)表的基礎(chǔ),查找出書(shū)的名稱。
重點(diǎn):通過(guò)book表的外鍵關(guān)聯(lián),查詢出該書(shū)的出版社名稱。
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})
相應(yīng)函數(shù)功能
def add_author(request):
if request.method == "POST":
new_author_name = request.POST.get("author_name")
#getlist方法,獲取所有選擇的書(shū)籍
books = request.POST.getlist("books")
#創(chuàng)建一個(gè)新的作者
new_author_obj = models.Author.objects.create(name=new_author_name)
#為該作者添加相應(yīng)的關(guān)系書(shū)籍,為在author_book表中添加相應(yīng)的記錄
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")
相應(yīng)的html代碼
展示效果:
def del_author(request):
#從URL值取到要?jiǎng)h除的作者id
delete_id = request.GET.get("id")
print(delete_id)
#根據(jù)ID值取到要?jiǎng)h除的對(duì)象,直接刪除
#1、去作者和書(shū)的關(guān)聯(lián)表,把對(duì)應(yīng)的關(guān)聯(lián)記錄刪除
#2、去作者表把作者刪除
models.Author.objects.get(id=delete_id).delete()
return redirect("/ormtest/author/")
def edit_author(request):
if request.method =="POST":
#拿到提交過(guò)來(lái)的編輯后的數(shù)據(jù)
edit_author_id = request.POST.get("author_id")
new_author_name = request.POST.get("author_name")
#拿到編輯后作者關(guān)聯(lián)的書(shū)籍信息
new_books = request.POST.getlist("books")
#根據(jù)ID找到當(dāng)前編輯的作者對(duì)象
edit_author_obj = models.Author.objects.get(id=edit_author_id)
#更新作者名字
edit_author_obj.name = new_author_name
#更新作者關(guān)聯(lián)的書(shū)的對(duì)應(yīng)關(guān)系
edit_author_obj.book.set(new_books)
#將修改提交到數(shù)據(jù)庫(kù)
edit_author_obj.save()
#返回作者列表頁(yè),查看是否編輯成功
return redirect("/ormtest/author/")
#html展示效果如添加頁(yè)面同樣