Django之ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系

實現(xiàn)環(huán)境表結(jié)構(gòu):

Django 之  ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系

站在用戶的角度思考問題,與客戶深入溝通,找到城東網(wǎng)站設(shè)計與城東網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、虛擬主機、企業(yè)郵箱。業(yè)務(wù)覆蓋城東地區(qū)。

models.py表單創(chuàng)建與代碼

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多對多關(guān)系

    def __str__(self):
        return "author_name:{}".format(self.name)

兩個HTML文件(book.html與author.html)

Django 之  ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系

重點:查詢author表,通過多對多的關(guān)系author_book這個表的基礎(chǔ),查找出書的名稱。

Django 之  ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系

重點:通過book表的外鍵關(guān)聯(lián),查詢出該書的出版社名稱。

views.py處理函數(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})

展示效果

Django 之  ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系

Django 之  ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系

Author跟Book表多對多關(guān)系的增刪改查方法

1、增加作者,并添加相應(yīng)的書籍

相應(yīng)函數(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)
        #為該作者添加相應(yīng)的關(guān)系書籍,為在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代碼

<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>

展示效果:
Django 之  ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系

2、刪除作者代碼:
def del_author(request):
    #從URL值取到要刪除的作者id
    delete_id = request.GET.get("id")
    print(delete_id)
    #根據(jù)ID值取到要刪除的對象,直接刪除
    #1、去作者和書的關(guān)聯(lián)表,把對應(yīng)的關(guān)聯(lián)記錄刪除
    #2、去作者表把作者刪除
    models.Author.objects.get(id=delete_id).delete()
    return redirect("/ormtest/author/")
3、編輯作者
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")
        #拿到編輯后作者關(guān)聯(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
        #更新作者關(guān)聯(lián)的書的對應(yīng)關(guān)系
        edit_author_obj.book.set(new_books)
        #將修改提交到數(shù)據(jù)庫
        edit_author_obj.save()
        #返回作者列表頁,查看是否編輯成功
        return redirect("/ormtest/author/")

#html展示效果如添加頁面同樣

文章名稱:Django之ORM表之間的外鍵關(guān)聯(lián)與多對多關(guān)系
當前鏈接:http://bm7419.com/article28/ijpccp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)面包屑導航、App開發(fā)、ChatGPT、定制開發(fā)、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)