Django中數(shù)據(jù)庫的數(shù)據(jù)關(guān)系:一對一,一對多,多對多
一對一:
一對一的關(guān)系極為一個數(shù)據(jù)僅對應(yīng)一個數(shù)據(jù),用下圖的結(jié)構(gòu)圖可以幫助理解:
下面用代碼實現(xiàn)一下,首先要創(chuàng)建工程項目如下:
接著,我們定義模型:
來到models.py
文件,創(chuàng)建兩個模型:
from django.db import models # Create your models here. class One(models.Model): oname = models.CharField(max_length=20,null=True) oage = models.CharField(max_length=20,null=True) odate = models.DateField(null=True) class Two(models.Model): # 設(shè)置一對一關(guān)系,是通過將表中的字段設(shè)置為主鍵完成的 # on_delete=models.CASCADE 當(dāng)父表中的某一條數(shù)據(jù)刪除的時候 # 相關(guān)字表中的數(shù)據(jù)也會被刪除 tsub = models.OneToOneField(One,on_delete=models.CASCADE,primary_key=True) tfond = models.CharField(max_length=20,null=True) tdes = models.CharField(max_length=200,null=True)
來到myPro
文件夾下添加以下兩句代碼:
import pymysql pymysql.install_as_MySQLdb()
下面可以遷移文件:
python manage.py makemigrations python manage.py migrate
這樣我們就創(chuàng)建了兩個表:
來到views.py
文件中添加數(shù)據(jù),代碼如下:
from django.shortcuts import render from .models import One,Two # Create your views here. def index(request): o1 = One.objects.create(oname='張三',oage=11,odate='2011-11-11') o2 = One.objects.create(oname='張三2',oage=12,odate='2012-12-12') t1 = Two.objects.create(tsub=o1,tfond='o1',tdes='我喜歡o1') t2 = Two.objects.create(tsub=o2,tfond='o2',tdes='我喜歡o2') return render(request,'index.html')
運行之后,將添加數(shù)據(jù)的代碼注釋掉,否則后面每運行一次都會添加。
下面,我們通過查詢數(shù)據(jù)來甄別其中的關(guān)系。
def select(request): t1 = Two.objects.get(tsub__oname = '張三') return render(request,'index.html',{'t1':t1})
一對多
即一個對象對應(yīng)著對個對象。
創(chuàng)建模型代碼:
from django.db import models # Create your models here. class People(models.Model): name = models.CharField(max_length=50) card_num = models.IntegerField(default=0) class Card(models.Model): number = models.CharField(max_length=20) person = models.ForeignKey(People,on_delete=models.CASCADE) source = models.CharField(max_length=50)
urls.py
路由設(shè)置:
from django.contrib import admin from django.urls import path from myApp import views urlpatterns = [ path('admin/', admin.site.urls), path('add/',views.add), path('select/',views.select), ]
views.py
文件中代碼:
from django.shortcuts import render from .models import People,Card from django.http import HttpResponse # Create your views here. # 添加數(shù)據(jù) def add(request): # p1 = People.objects.create(name='小王',card_num = 4) # p2 = People.objects.create(name='老王', card_num=40) # # c1 = Card(number='101',source = '中國銀行',person = p1) # c2 = Card(number='102', source='中國農(nóng)行', person=p1) # c3 = Card(number='110', source='中國建行', person=p1) # c1.save() # c2.save() # c3.save() # # c4 = Card(number='201', source='河南鄭州美容美發(fā)', person=p2) # c5 = Card(number='202', source='鄭州交通一卡通', person=p2) # c6 = Card(number='203', source='鄭州逍遙鎮(zhèn)胡辣湯', person=p2) # c7 = Card(number='204', source='鄭州惠濟(jì)四附院', person=p2) # # c4.save() # c5.save() # c6.save() # c7.save() return HttpResponse('添加成功') def select(request): # 查找number=203的人 c1 = Card.objects.get(number='203') print(c1.person.name) # 查找id為3對應(yīng)的人 c2 = Card.objects.get(id=3) print(c2.person.name) # 查找c2的所有卡 result = c2.person.card_set.all() print(result) for res in result: print(res.source) # 查找名字為老王的所有卡種 result = People.objects.get(name='老王') for card in result.card_set.all(): print(card.source) return HttpResponse('查詢成功')
多對多
即多個對象對應(yīng)對個對象,類似公交車坐車,人可以坐多個公交車,公交車也可以載不同的人。
創(chuàng)建模型代碼如下:
from django.db import models # Create your models here. class Publication(models.Model): pname = models.CharField(max_length=200) paddress = models.CharField(max_length=200) class Book(models.Model): bname = models.CharField(max_length=200) bauthor = models.CharField(max_length=200) publication = models.ManyToManyField(Publication)
視圖文件views.py
文件代碼如下:
from django.shortcuts import render from .models import Publication,Book from django.http import HttpResponse # Create your views here. def add(request): # p1 = Publication(pname='大象出版社',paddress='河南',) # p2 = Publication(pname='北京出版社',paddress='北京') # p3 = Publication(pname='清華出版社',paddress='河北') # p1.save() # p2.save() # p3.save() # # b1 = Book(bname='海底兩萬里',bauthor='趙四') # b2 = Book(bname='遮天',bauthor='辰東') # b3 = Book(bname='童年', bauthor='xxxx') # b4 = Book(bname='在人間', bauthor='yyyy') # b5 = Book(bname='我的大學(xué)', bauthor='張飛') # b6 = Book(bname='湯姆索亞歷險記', bauthor='趙六兒') # b1.save() # b2.save() # b3.save() # b4.save() # b5.save() # b6.save() # # b1.publication.add(p1,p2,p3) # b2.publication.add(p1,p2) # b3.publication.add(p1,p3) # b4.publication.add(p2,p3) # b5.publication.add(p3) # 多對多關(guān)系,兩個表不直接產(chǎn)生聯(lián)系,而是將兩個表之間的關(guān)系記錄在中間表上 # 中間表不需要創(chuàng)建,會自動生成 return HttpResponse('添加成功') def select(request): # 通過書籍查找對應(yīng)的出版社 b1 = Book.objects.get(bname='童年') # 獲取出版童年的所有出版社 b1_publication = b1.publication.all() for pub in b1_publication: print(pub.pname) print(pub.paddress) p1 = Publication.objects.get(pname = '清華出版社') all_book = p1.book_set.all() print('------------------') for book in all_book: print(book.bname) print(book.bauthor) return HttpResponse('查找成功')
這樣,就介紹完了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- 詳解多線程Django程序耗盡數(shù)據(jù)庫連接的問題
- Django使用多數(shù)據(jù)庫的方法
- django 多數(shù)據(jù)庫配置教程
- Django app配置多個數(shù)據(jù)庫代碼實例
- Django多數(shù)據(jù)庫配置及逆向生成model教程
- django 多數(shù)據(jù)庫及分庫實現(xiàn)方式
- django 鏈接多個數(shù)據(jù)庫 并使用原生sql實現(xiàn)
- Django多數(shù)據(jù)庫的實現(xiàn)過程詳解
- Django多數(shù)據(jù)庫聯(lián)用實現(xiàn)方法解析
- django使用多個數(shù)據(jù)庫的方法實例
相關(guān)文章
Python連接數(shù)據(jù)庫學(xué)習(xí)之DB-API詳解
在沒有 Python DB-API 之前,各數(shù)據(jù)庫之間的應(yīng)用接口非?;靵y,實現(xiàn)各不相同。如果項目需要更換數(shù)據(jù)庫時,則需要做大量的修改,非常不便。Python DB-API 的出現(xiàn)就是為了解決這樣的問題。本文主要介紹了Python連接數(shù)據(jù)庫之DB-API的相關(guān)資料,需要的朋友可以參考。2017-02-02利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的操作實例
這篇文章主要給大家介紹了關(guān)于利用Python批量導(dǎo)出mysql數(shù)據(jù)庫表結(jié)構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-08-08Python之tkinter面板PanedWindow的使用
這篇文章主要介紹了Python之tkinter面板PanedWindow的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05用python結(jié)合jieba和wordcloud實現(xiàn)詞云效果
詞云,顧名思義就是很多個單詞,然后通過出現(xiàn)的頻率或者比重之類的標(biāo)準(zhǔn)匯聚成一個云朵的樣子嘛,其實呢現(xiàn)在網(wǎng)上已經(jīng)有很多能自動生成詞云的工具了,比如Wordle,Tagxedo等等,Python也能實現(xiàn)這樣的效果,我們通過jieba庫和wordcloud庫也能十分輕松的完成詞云的構(gòu)建2017-09-09centos 下面安裝python2.7 +pip +mysqld
這篇文章主要介紹了centos 下面安裝python2.7 +pip +mysqld,需要的朋友可以參考下2014-11-11