關係型數據庫的關係包括三種類型:html
#定義圖書模型類BookInfo class BookInfo(models.Model): btitle = models.CharField(max_length=20)#圖書名稱 bpub_date = models.DateField()#發佈日期 bread = models.IntegerField(default=0)#閱讀量 bcomment = models.IntegerField(default=0)#評論量 isDelete = models.BooleanField(default=False)#邏輯刪除 #定義角色模型類RoleInfo class RoleInfo(models.Model): rname = models.CharField(max_length=20)#角色姓名 rgender = models.BooleanField(default=True)#角色性別 isDelete = models.BooleanField(default=False)#邏輯刪除 rcomment = models.CharField(max_len角色與圖書表的關係爲一對多,因此屬性定義在角色模型類中
咱們下面設計一個新聞類和新聞類型類,一個新聞類型下能夠用不少條新聞,一條新聞也可能歸屬於多種新聞類型。python
class TypeInfo(models.Model): tname = models.CharField(max_length=20) #新聞類別 class NewsInfo(models.Model): ntitle = models.CharField(max_length=60) #新聞標題 ncontent = models.TextField() #新聞內容 npub_date = models.DateTimeField(auto_now_add=True) #新聞發佈時間 ntype = models.ManyToManyField('TypeInfo') #經過ManyToManyField創建TypeInfo類和NewsInfo類之間多對多的關係
Django中也能實現相似於join查詢。mysql
在定義模型類時,能夠指定三種關聯關係,最經常使用的是一對多關係,例如「圖書-角色」就是一對多關係。sql
#一對應的模型類對象.多對應的模型類名小寫_set b = BookInfo.objects.get(id=1) b.heroinfo_set.all()
由多到一的訪問語法:數據庫
#多對應的模型類對象.多對應的模型類中的關係類屬性名 r = RoleInfo.objects.get(id=1) r.rbook
訪問一對應的模型類關聯對象的id語法:瀏覽器
#多對應的模型類對象.關聯類屬性_id r = RoleInfo.objects.get(id=1) r.book_id
例:查詢編號爲1的圖書。服務器
book=BookInfo.objects.get(pk=1)
例:得到book圖書的全部角色。app
book.roleinfo_set.all()
例:得到編號爲1的角色。ide
role=RoleInfo.objects.get(pk=1)
例:得到role角色出自的圖書。url
role.rbook
由多模型類條件查詢一模型類數據:
語法以下:
關聯模型類名小寫__屬性名__條件運算符=值
若是沒有"__運算符"部分,表示等於,結果和sql中的inner join相同。
例:查詢圖書,要求圖書中英雄的描述包含'韓'。
list = BookInfo.objects.filter(heroinfo__hcontent__contains='韓')
由一模型類條件查詢多模型類數據:
語法以下:
一模型類關聯屬性名__一模型類屬性名__條件運算符=值
例:查詢書名爲「斗羅大陸」的全部英雄。
list = HeroInfo.objects.filter(hbook__btitle='斗羅大陸')
對於地區信息、分類信息等數據,表結構很是相似,每一個表的數據量十分有限,爲了充分利用數據表的大量數據存儲功能,能夠設計成一張表,內部的關係字段指向本表的主鍵,這就是自關聯的表結構。
打開booktest/models.py文件,定義AreaInfo類。
關係屬性使用self指向本類,要求null和blank容許爲空,由於一級數據是沒有父級的。
#定義地區模型類,存儲省、市、區縣信息 class AreaInfo(models.Model): atitle=models.CharField(max_length=30)#名稱 aParent=models.ForeignKey('self',null=True,blank=True)#關係
遷移。
python manage.py makemigrations
python manage.py migrate
打開mysql命令行,導入數據。
INSERT INTO booktest_areainfo VALUES ('440000', '廣東省', NULL); INSERT INTO booktest_areainfo VALUES ('440100', '廣州市', '440000'); INSERT INTO booktest_areainfo VALUES ('440103', '荔灣區', '440100'); INSERT INTO booktest_areainfo VALUES ('440104', '越秀區', '440100'); INSERT INTO booktest_areainfo VALUES ('440105', '海珠區', '440100'); INSERT INTO booktest_areainfo VALUES ('440106', '天河區', '440100'); INSERT INTO booktest_areainfo VALUES ('440111', '白雲區', '440100'); INSERT INTO booktest_areainfo VALUES ('440112', '黃埔區', '440100'); INSERT INTO booktest_areainfo VALUES ('440113', '番禺區', '440100'); INSERT INTO booktest_areainfo VALUES ('440114', '花都區', '440100'); INSERT INTO booktest_areainfo VALUES ('440115', '南沙區', '440100'); INSERT INTO booktest_areainfo VALUES ('440116', '蘿崗區', '440100'); INSERT INTO booktest_areainfo VALUES ('440183', '增城市', '440100'); INSERT INTO booktest_areainfo VALUES ('440184', '從化市', '440100'); INSERT INTO booktest_areainfo VALUES ('440200', '韶關市', '440000'); INSERT INTO booktest_areainfo VALUES ('440203', '武江區', '440200'); INSERT INTO booktest_areainfo VALUES ('440204', '湞江區', '440200'); INSERT INTO booktest_areainfo VALUES ('440205', '曲江區', '440200'); INSERT INTO booktest_areainfo VALUES ('440222', '始興縣', '440200'); INSERT INTO booktest_areainfo VALUES ('440224', '仁化縣', '440200'); INSERT INTO booktest_areainfo VALUES ('440229', '翁源縣', '440200'); INSERT INTO booktest_areainfo VALUES ('440232', '乳源瑤族自治縣', '440200'); INSERT INTO booktest_areainfo VALUES ('440233', '新豐縣', '440200'); INSERT INTO booktest_areainfo VALUES ('440281', '樂昌市', '440200'); INSERT INTO booktest_areainfo VALUES ('440282', '南雄市', '440200'); INSERT INTO booktest_areainfo VALUES ('440300', '深圳市', '440000'); INSERT INTO booktest_areainfo VALUES ('440303', '羅湖區', '440300'); INSERT INTO booktest_areainfo VALUES ('440304', '福田區', '440300'); INSERT INTO booktest_areainfo VALUES ('440305', '南山區', '440300'); INSERT INTO booktest_areainfo VALUES ('440306', '寶安區', '440300'); INSERT INTO booktest_areainfo VALUES ('440307', '龍崗區', '440300'); INSERT INTO booktest_areainfo VALUES ('440308', '鹽田區', '440300'); INSERT INTO booktest_areainfo VALUES ('440400', '珠海市', '440000'); INSERT INTO booktest_areainfo VALUES ('440402', '香洲區', '440400'); INSERT INTO booktest_areainfo VALUES ('440403', '斗門區', '440400'); INSERT INTO booktest_areainfo VALUES ('440404', '金灣區', '440400'); INSERT INTO booktest_areainfo VALUES ('440500', '汕頭市', '440000'); INSERT INTO booktest_areainfo VALUES ('440507', '龍湖區', '440500'); INSERT INTO booktest_areainfo VALUES ('440511', '金平區', '440500'); INSERT INTO booktest_areainfo VALUES ('440512', '濠江區', '440500'); INSERT INTO booktest_areainfo VALUES ('440513', '潮陽區', '440500'); INSERT INTO booktest_areainfo VALUES ('440514', '潮南區', '440500'); INSERT INTO booktest_areainfo VALUES ('440515', '澄海區', '440500'); INSERT INTO booktest_areainfo VALUES ('440523', '南澳縣', '440500'); INSERT INTO booktest_areainfo VALUES ('440600', '佛山市', '440000'); INSERT INTO booktest_areainfo VALUES ('440604', '禪城區', '440600'); INSERT INTO booktest_areainfo VALUES ('440605', '南海區', '440600'); INSERT INTO booktest_areainfo VALUES ('440606', '順德區', '440600'); INSERT INTO booktest_areainfo VALUES ('440607', '三水區', '440600'); INSERT INTO booktest_areainfo VALUES ('440608', '高明區', '440600'); INSERT INTO booktest_areainfo VALUES ('440700', '江門市', '440000'); INSERT INTO booktest_areainfo VALUES ('440703', '蓬江區', '440700'); INSERT INTO booktest_areainfo VALUES ('440704', '江海區', '440700'); INSERT INTO booktest_areainfo VALUES ('440705', '新會區', '440700'); INSERT INTO booktest_areainfo VALUES ('440781', '臺山市', '440700'); INSERT INTO booktest_areainfo VALUES ('440783', '開平市', '440700'); INSERT INTO booktest_areainfo VALUES ('440784', '鶴山市', '440700'); INSERT INTO booktest_areainfo VALUES ('440785', '恩平市', '440700'); INSERT INTO booktest_areainfo VALUES ('440800', '湛江市', '440000'); INSERT INTO booktest_areainfo VALUES ('440802', '赤坎區', '440800'); INSERT INTO booktest_areainfo VALUES ('440803', '霞山區', '440800'); INSERT INTO booktest_areainfo VALUES ('440804', '坡頭區', '440800'); INSERT INTO booktest_areainfo VALUES ('440811', '麻章區', '440800'); INSERT INTO booktest_areainfo VALUES ('440823', '遂溪縣', '440800'); INSERT INTO booktest_areainfo VALUES ('440825', '徐聞縣', '440800'); INSERT INTO booktest_areainfo VALUES ('440881', '廉江市', '440800'); INSERT INTO booktest_areainfo VALUES ('440882', '雷州市', '440800'); INSERT INTO booktest_areainfo VALUES ('440883', '吳川市', '440800'); INSERT INTO booktest_areainfo VALUES ('440900', '茂名市', '440000'); INSERT INTO booktest_areainfo VALUES ('440902', '茂南區', '440900'); INSERT INTO booktest_areainfo VALUES ('440903', '茂港區', '440900'); INSERT INTO booktest_areainfo VALUES ('440923', '電白縣', '440900'); INSERT INTO booktest_areainfo VALUES ('440981', '高州市', '440900'); INSERT INTO booktest_areainfo VALUES ('440982', '化州市', '440900'); INSERT INTO booktest_areainfo VALUES ('440983', '信宜市', '440900'); INSERT INTO booktest_areainfo VALUES ('441200', '肇慶市', '440000'); INSERT INTO booktest_areainfo VALUES ('441202', '端州區', '441200'); INSERT INTO booktest_areainfo VALUES ('441203', '鼎湖區', '441200'); INSERT INTO booktest_areainfo VALUES ('441223', '廣寧縣', '441200'); INSERT INTO booktest_areainfo VALUES ('441224', '懷集縣', '441200'); INSERT INTO booktest_areainfo VALUES ('441225', '封開縣', '441200'); INSERT INTO booktest_areainfo VALUES ('441226', '德慶縣', '441200'); INSERT INTO booktest_areainfo VALUES ('441283', '高要市', '441200'); INSERT INTO booktest_areainfo VALUES ('441284', '四會市', '441200'); INSERT INTO booktest_areainfo VALUES ('441300', '惠州市', '440000'); INSERT INTO booktest_areainfo VALUES ('441302', '惠城區', '441300'); INSERT INTO booktest_areainfo VALUES ('441303', '惠陽區', '441300'); INSERT INTO booktest_areainfo VALUES ('441322', '博羅縣', '441300'); INSERT INTO booktest_areainfo VALUES ('441323', '惠東縣', '441300'); INSERT INTO booktest_areainfo VALUES ('441324', '龍門縣', '441300'); INSERT INTO booktest_areainfo VALUES ('441400', '梅州市', '440000'); INSERT INTO booktest_areainfo VALUES ('441402', '梅江區', '441400'); INSERT INTO booktest_areainfo VALUES ('441421', '梅縣', '441400'); INSERT INTO booktest_areainfo VALUES ('441422', '大埔縣', '441400'); INSERT INTO booktest_areainfo VALUES ('441423', '豐順縣', '441400'); INSERT INTO booktest_areainfo VALUES ('441424', '五華縣', '441400'); INSERT INTO booktest_areainfo VALUES ('441426', '平遠縣', '441400'); INSERT INTO booktest_areainfo VALUES ('441427', '蕉嶺縣', '441400'); INSERT INTO booktest_areainfo VALUES ('441481', '興寧市', '441400'); INSERT INTO booktest_areainfo VALUES ('441500', '汕尾市', '440000'); INSERT INTO booktest_areainfo VALUES ('441502', '城區', '441500'); INSERT INTO booktest_areainfo VALUES ('441521', '海豐縣', '441500'); INSERT INTO booktest_areainfo VALUES ('441523', '陸河縣', '441500'); INSERT INTO booktest_areainfo VALUES ('441581', '陸豐市', '441500'); INSERT INTO booktest_areainfo VALUES ('441600', '河源市', '440000'); INSERT INTO booktest_areainfo VALUES ('441602', '源城區', '441600'); INSERT INTO booktest_areainfo VALUES ('441621', '紫金縣', '441600'); INSERT INTO booktest_areainfo VALUES ('441622', '龍川縣', '441600'); INSERT INTO booktest_areainfo VALUES ('441623', '連平縣', '441600'); INSERT INTO booktest_areainfo VALUES ('441624', '和平縣', '441600'); INSERT INTO booktest_areainfo VALUES ('441625', '東源縣', '441600'); INSERT INTO booktest_areainfo VALUES ('441700', '陽江市', '440000'); INSERT INTO booktest_areainfo VALUES ('441702', '江城區', '441700'); INSERT INTO booktest_areainfo VALUES ('441721', '陽西縣', '441700'); INSERT INTO booktest_areainfo VALUES ('441723', '陽東縣', '441700'); INSERT INTO booktest_areainfo VALUES ('441781', '陽春市', '441700'); INSERT INTO booktest_areainfo VALUES ('441800', '清遠市', '440000'); INSERT INTO booktest_areainfo VALUES ('441802', '清城區', '441800'); INSERT INTO booktest_areainfo VALUES ('441821', '佛岡縣', '441800'); INSERT INTO booktest_areainfo VALUES ('441823', '陽山縣', '441800'); INSERT INTO booktest_areainfo VALUES ('441825', '連山壯族瑤族自治縣', '441800'); INSERT INTO booktest_areainfo VALUES ('441826', '連南瑤族自治縣', '441800'); INSERT INTO booktest_areainfo VALUES ('441827', '清新縣', '441800'); INSERT INTO booktest_areainfo VALUES ('441881', '英德市', '441800'); INSERT INTO booktest_areainfo VALUES ('441882', '連州市', '441800'); INSERT INTO booktest_areainfo VALUES ('441900', '東莞市', '440000'); INSERT INTO booktest_areainfo VALUES ('442000', '中山市', '440000'); INSERT INTO booktest_areainfo VALUES ('445100', '潮州市', '440000'); INSERT INTO booktest_areainfo VALUES ('445102', '湘橋區', '445100'); INSERT INTO booktest_areainfo VALUES ('445121', '潮安區', '445100'); INSERT INTO booktest_areainfo VALUES ('445122', '饒平縣', '445100'); INSERT INTO booktest_areainfo VALUES ('445200', '揭陽市', '440000'); INSERT INTO booktest_areainfo VALUES ('445202', '榕城區', '445200'); INSERT INTO booktest_areainfo VALUES ('445221', '揭東縣', '445200'); INSERT INTO booktest_areainfo VALUES ('445222', '揭西縣', '445200'); INSERT INTO booktest_areainfo VALUES ('445224', '惠來縣', '445200'); INSERT INTO booktest_areainfo VALUES ('445281', '普寧市', '445200'); INSERT INTO booktest_areainfo VALUES ('445300', '雲浮市', '440000'); INSERT INTO booktest_areainfo VALUES ('445302', '云城區', '445300'); INSERT INTO booktest_areainfo VALUES ('445321', '新興縣', '445300'); INSERT INTO booktest_areainfo VALUES ('445322', '鬱南縣', '445300'); INSERT INTO booktest_areainfo VALUES ('445323', '雲安縣', '445300'); INSERT INTO booktest_areainfo VALUES ('445381', '羅定市', '445300'); INSERT INTO booktest_areainfo VALUES ('430000', '湖南省', NULL); INSERT INTO booktest_areainfo VALUES ('430100', '長沙市', '430000'); INSERT INTO booktest_areainfo VALUES ('430102', '芙蓉區', '430100'); INSERT INTO booktest_areainfo VALUES ('430103', '天心區', '430100'); INSERT INTO booktest_areainfo VALUES ('430104', '嶽麓區', '430100'); INSERT INTO booktest_areainfo VALUES ('430105', '開福區', '430100'); INSERT INTO booktest_areainfo VALUES ('430111', '雨花區', '430100'); INSERT INTO booktest_areainfo VALUES ('430121', '長沙縣', '430100'); INSERT INTO booktest_areainfo VALUES ('430122', '望城縣', '430100'); INSERT INTO booktest_areainfo VALUES ('430124', '寧鄉縣', '430100'); INSERT INTO booktest_areainfo VALUES ('430181', '瀏陽市', '430100'); INSERT INTO booktest_areainfo VALUES ('430200', '株洲市', '430000'); INSERT INTO booktest_areainfo VALUES ('430202', '荷塘區', '430200'); INSERT INTO booktest_areainfo VALUES ('430203', '蘆淞區', '430200'); INSERT INTO booktest_areainfo VALUES ('430204', '石峯區', '430200'); INSERT INTO booktest_areainfo VALUES ('430211', '天元區', '430200'); INSERT INTO booktest_areainfo VALUES ('430221', '株洲縣', '430200'); INSERT INTO booktest_areainfo VALUES ('430223', '攸縣', '430200'); INSERT INTO booktest_areainfo VALUES ('430224', '茶陵縣', '430200'); INSERT INTO booktest_areainfo VALUES ('430225', '炎陵縣', '430200'); INSERT INTO booktest_areainfo VALUES ('430281', '醴陵市', '430200'); INSERT INTO booktest_areainfo VALUES ('430300', '湘潭市', '430000'); INSERT INTO booktest_areainfo VALUES ('430302', '雨湖區', '430300'); INSERT INTO booktest_areainfo VALUES ('430304', '嶽塘區', '430300'); INSERT INTO booktest_areainfo VALUES ('430321', '湘潭縣', '430300'); INSERT INTO booktest_areainfo VALUES ('430381', '湘鄉市', '430300'); INSERT INTO booktest_areainfo VALUES ('430382', '韶山市', '430300'); INSERT INTO booktest_areainfo VALUES ('430400', '衡陽市', '430000'); INSERT INTO booktest_areainfo VALUES ('430405', '珠暉區', '430400'); INSERT INTO booktest_areainfo VALUES ('430406', '雁峯區', '430400'); INSERT INTO booktest_areainfo VALUES ('430407', '石鼓區', '430400'); INSERT INTO booktest_areainfo VALUES ('430408', '蒸湘區', '430400'); INSERT INTO booktest_areainfo VALUES ('430412', '南嶽區', '430400'); INSERT INTO booktest_areainfo VALUES ('430421', '衡陽縣', '430400'); INSERT INTO booktest_areainfo VALUES ('430422', '衡南縣', '430400'); INSERT INTO booktest_areainfo VALUES ('430423', '衡山縣', '430400'); INSERT INTO booktest_areainfo VALUES ('430424', '衡東縣', '430400'); INSERT INTO booktest_areainfo VALUES ('430426', '祁東縣', '430400'); INSERT INTO booktest_areainfo VALUES ('430481', '耒陽市', '430400'); INSERT INTO booktest_areainfo VALUES ('430482', '常寧市', '430400'); INSERT INTO booktest_areainfo VALUES ('430500', '邵陽市', '430000'); INSERT INTO booktest_areainfo VALUES ('430502', '雙清區', '430500'); INSERT INTO booktest_areainfo VALUES ('430503', '大祥區', '430500'); INSERT INTO booktest_areainfo VALUES ('430511', '北塔區', '430500'); INSERT INTO booktest_areainfo VALUES ('430521', '邵東縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430522', '新邵縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430523', '邵陽縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430524', '隆回縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430525', '洞口縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430527', '綏寧縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430528', '新寧縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430529', '城步苗族自治縣', '430500'); INSERT INTO booktest_areainfo VALUES ('430581', '武岡市', '430500'); INSERT INTO booktest_areainfo VALUES ('430600', '岳陽市', '430000'); INSERT INTO booktest_areainfo VALUES ('430602', '岳陽樓區', '430600'); INSERT INTO booktest_areainfo VALUES ('430603', '云溪區', '430600'); INSERT INTO booktest_areainfo VALUES ('430611', '君山區', '430600'); INSERT INTO booktest_areainfo VALUES ('430621', '岳陽縣', '430600'); INSERT INTO booktest_areainfo VALUES ('430623', '華容縣', '430600'); INSERT INTO booktest_areainfo VALUES ('430624', '湘陰縣', '430600'); INSERT INTO booktest_areainfo VALUES ('430626', '平江縣', '430600'); INSERT INTO booktest_areainfo VALUES ('430681', '汨羅市', '430600'); INSERT INTO booktest_areainfo VALUES ('430682', '臨湘市', '430600'); INSERT INTO booktest_areainfo VALUES ('430700', '常德市', '430000'); INSERT INTO booktest_areainfo VALUES ('430702', '武陵區', '430700'); INSERT INTO booktest_areainfo VALUES ('430703', '鼎城區', '430700'); INSERT INTO booktest_areainfo VALUES ('430721', '安鄉縣', '430700'); INSERT INTO booktest_areainfo VALUES ('430722', '漢壽縣', '430700'); INSERT INTO booktest_areainfo VALUES ('430723', '澧縣', '430700'); INSERT INTO booktest_areainfo VALUES ('430724', '臨澧縣', '430700'); INSERT INTO booktest_areainfo VALUES ('430725', '桃源縣', '430700'); INSERT INTO booktest_areainfo VALUES ('430726', '石門縣', '430700'); INSERT INTO booktest_areainfo VALUES ('430781', '津市市', '430700'); INSERT INTO booktest_areainfo VALUES ('430800', '張家界市', '430000'); INSERT INTO booktest_areainfo VALUES ('430802', '永定區', '430800'); INSERT INTO booktest_areainfo VALUES ('430811', '武陵源區', '430800'); INSERT INTO booktest_areainfo VALUES ('430821', '慈利縣', '430800'); INSERT INTO booktest_areainfo VALUES ('430822', '桑植縣', '430800'); INSERT INTO booktest_areainfo VALUES ('430900', '益陽市', '430000'); INSERT INTO booktest_areainfo VALUES ('430902', '資陽區', '430900'); INSERT INTO booktest_areainfo VALUES ('430903', '赫山區', '430900'); INSERT INTO booktest_areainfo VALUES ('430921', '南縣', '430900'); INSERT INTO booktest_areainfo VALUES ('430922', '桃江縣', '430900'); INSERT INTO booktest_areainfo VALUES ('430923', '安化縣', '430900'); INSERT INTO booktest_areainfo VALUES ('430981', '沅江市', '430900'); INSERT INTO booktest_areainfo VALUES ('431000', '郴州市', '430000'); INSERT INTO booktest_areainfo VALUES ('431002', '北湖區', '431000'); INSERT INTO booktest_areainfo VALUES ('431003', '甦仙區', '431000'); INSERT INTO booktest_areainfo VALUES ('431021', '桂陽縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431022', '宜章縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431023', '永興縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431024', '嘉禾縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431025', '臨武縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431026', '汝城縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431027', '桂東縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431028', '安仁縣', '431000'); INSERT INTO booktest_areainfo VALUES ('431081', '資興市', '431000'); INSERT INTO booktest_areainfo VALUES ('431100', '永州市', '430000'); INSERT INTO booktest_areainfo VALUES ('431102', '零陵區', '431100'); INSERT INTO booktest_areainfo VALUES ('431103', '冷水灘區', '431100'); INSERT INTO booktest_areainfo VALUES ('431121', '祁陽縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431122', '東安縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431123', '雙牌縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431124', '道縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431125', '江永縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431126', '寧遠縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431127', '藍山縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431128', '新田縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431129', '江華瑤族自治縣', '431100'); INSERT INTO booktest_areainfo VALUES ('431200', '懷化市', '430000'); INSERT INTO booktest_areainfo VALUES ('431202', '鶴城區', '431200'); INSERT INTO booktest_areainfo VALUES ('431221', '中方縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431222', '沅陵縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431223', '辰溪縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431224', '漵浦縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431225', '會同縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431226', '麻陽苗族自治縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431227', '新晃侗族自治縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431228', '芷江侗族自治縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431229', '靖州苗族侗族自治縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431230', '通道侗族自治縣', '431200'); INSERT INTO booktest_areainfo VALUES ('431281', '洪江市', '431200'); INSERT INTO booktest_areainfo VALUES ('431300', '婁底市', '430000'); INSERT INTO booktest_areainfo VALUES ('431302', '婁星區', '431300'); INSERT INTO booktest_areainfo VALUES ('431321', '雙峯縣', '431300'); INSERT INTO booktest_areainfo VALUES ('431322', '新化縣', '431300'); INSERT INTO booktest_areainfo VALUES ('431381', '冷水江市', '431300'); INSERT INTO booktest_areainfo VALUES ('431382', '漣源市', '431300'); INSERT INTO booktest_areainfo VALUES ('433100', '湘西土家族苗族自治州', '430000'); INSERT INTO booktest_areainfo VALUES ('433101', '吉首市', '433100'); INSERT INTO booktest_areainfo VALUES ('433122', '瀘溪縣', '433100'); INSERT INTO booktest_areainfo VALUES ('433123', '鳳凰縣', '433100'); INSERT INTO booktest_areainfo VALUES ('433124', '花垣縣', '433100'); INSERT INTO booktest_areainfo VALUES ('433125', '保靖縣', '433100'); INSERT INTO booktest_areainfo VALUES ('433126', '古丈縣', '433100'); INSERT INTO booktest_areainfo VALUES ('433127', '永順縣', '433100'); INSERT INTO booktest_areainfo VALUES ('433130', '龍山縣', '433100');
source areas.sql
打開booktest/views.py文件,定義視圖area。
from booktest.models import AreaInfo ... #查詢廣州市的信息 def area(request): area = AreaInfo.objects.get(pk=440100) return render(request, 'booktest/area.html', {'area': area})
打開booktest/urls.py文件,新建一條url。
urlpatterns = [ ... url(r'^area/$', views.area), ]
在templates/booktest目錄下,新建area.html文件。
<html> <head> <title>地區</title> </head> <body> 當前地區:{{area.atitle}} <hr/> 上級地區:{{area.aParent.atitle}} <hr/> 下級地區: <ul> {%for a in area.areainfo_set.all%} <li>{{a.atitle}}</li> {%endfor%} </ul> </body> </html>
運行服務器。
python manage.py runserver
在瀏覽器中輸出效果以下圖。
屬性objects:管理器,是models.Manager類型的對象,用於與數據庫進行交互。
當沒有爲模型類定義管理器時,Django會爲每個模型類生成一個名爲objects的管理器,自定義管理器後,Django再也不生成默認管理器objects。
爲模型類BookInfo定義管理器books語法以下:
class BookInfo(models.Model): ... books = models.Manager()
管理器是Django的模型進行數據庫操做的接口,Django應用的每一個模型類都擁有至少一個管理器。Django支持自定義管理器類,繼承自models.Manager。
自定義管理器類主要用於兩種狀況:
1.修改原始查詢集,重寫all()方法。
a)打開booktest/models.py文件,定義類BookInfoManager
#圖書管理器 class BookInfoManager(models.Manager): def all(self): #默認查詢未刪除的圖書信息 #調用父類的成員語法爲:super().方法名 return super().all().filter(isDelete=False)
b)在模型類BookInfo中定義管理器
class BookInfo(models.Model): ... books = BookInfoManager()
2.在管理器類中定義建立對象的方法
對模型類對應的數據表進行操做時,推薦將這些操做數據表的方法封裝起來,放到模型管理器類中。
a)打開booktest/models.py文件,定義方法create。
class BookInfoManager(models.Manager): ... #建立模型類,接收參數爲屬性賦值 def create_book(self, title, pub_date): #建立模型類對象self.model能夠得到模型類 book = self.model() book.btitle = title book.bpub_date = pub_date book.bread=0 book.bcommet=0 book.isDelete = False # 將數據插入進數據表 book.save() return book
b)爲模型類BookInfo定義管理器books語法以下:
class BookInfo(models.Model): ... books = BookInfoManager()
c)調用語法以下:
調用:book=BookInfo.books.create_book("abc",date(1980,1,1))
在模型類中定義類Meta,用於設置元信息,如使用db_table自定義表的名字。
數據表的默認名稱爲:
<app_name>_<model_name>
例:
booktest_bookinfo
例:指定BookInfo模型類生成的數據表名爲bookinfo。
在BookInfo模型類中添加以下內容,代碼以下:
#定義圖書模型類BookInfo class BookInfo(models.Model): ... #定義元選項 class Meta: db_table='bookinfo' #指定BookInfo生成的數據表名爲bookinfo