django框架面向?qū)ο驩RM模型繼承用法實例分析
本文實例講述了django框架面向?qū)ο驩RM模型繼承用法。分享給大家供大家參考,具體如下:
Django ORM對模型繼承的支持,將python面向?qū)ο蟮木幊谭椒ㄅc數(shù)據(jù)庫面向關(guān)系表的數(shù)據(jù)結(jié)構(gòu)結(jié)合的很巧妙。支持三種風格的模型繼承。
1.抽象類繼承:父類繼承自models.Model,但不會在底層數(shù)據(jù)庫中生成相應(yīng)的數(shù)據(jù)表,父類的屬性列存儲在其子類的數(shù)據(jù)表中。
2.多表繼承:多表繼承的每個模型類都在底層數(shù)據(jù)庫中生成相應(yīng)的數(shù)據(jù)表管理數(shù)據(jù)。
3.代理模型繼承:父類用于在底層數(shù)據(jù)庫中管理數(shù)據(jù)表,而子類不定義數(shù)據(jù)列,只定義查詢數(shù)據(jù)表的排序方式等元數(shù)據(jù)。
1.抽象基類繼承舉例如下:
from django.db import models class Message(models.Model): id = models.AutoField(default=0) content = models.CharField(max_length=100) user_name = models.CharField(max_length=80) pub_date = models.DateField() class Meta: abstract = True #證明該類是抽象基類 class Moment(Message): headline = models.CharField(max_length=50) LEVELS = ( ('1', 'Very Good'), ('2', 'Good'), ('3', 'Normal'), ('4', 'Bad'), ) class Coment(Message): level = models.CharField(max_length=50, choices=LEVELS, default=LEVELS[3])
抽象基類Message,用于保存消息的四個字段:id、content、user_name和pub_date。子類Moment和Coment分別繼承Message,并且分別定義獨有的一個地段。三個類映射到數(shù)據(jù)庫,會被定義為兩個數(shù)據(jù)表(抽象基類不在底層數(shù)據(jù)庫中聲稱數(shù)據(jù)表):
數(shù)據(jù)表:app_Moment:app代表應(yīng)用,包括id、user_name、content、pub_date和headline五個字段
數(shù)據(jù)表:app_Coment: 包括包括id、user_name、content、pub_date和level五個字段
2.多表繼承
from django.db import models class Message(models.Model): id = models.AutoField(default=0) content = models.CharField(max_length=100) user_name = models.CharField(max_length=80) pub_date = models.DateField() class Moment(Message): headline = models.CharField(max_length=50) LEVELS = ( ('1', 'Very Good'), ('2', 'Good'), ('3', 'Normal'), ('4', 'Bad'), ) class Coment(Message): level = models.CharField(max_length=50, choices=LEVELS, default=LEVELS[3])
類Message,用于保存消息的四個字段:id、content、user_name和pub_date。子類Moment和Coment分別繼承Message,并且分別定義獨有的一個字段。三個類映射到數(shù)據(jù)庫,會被定義為三個數(shù)據(jù)表:
數(shù)據(jù)表:app_Moment:app代表應(yīng)用,包括id、user_name、content、pub_date和headline五個字段
數(shù)據(jù)表:app_Coment: 包括包括id、user_name、content、pub_date和level五個字段
數(shù)據(jù)表:app_Message:包括包括id、user_name、content、pub_date四個字段
多表繼承時,在子類實例中可以通過小寫的父類名字引用父類的實力
e.g:
al = Moment(xx, user_name="test") print(al.message.user_name) #-------> test
3.代理模型繼承
前兩種繼承模型中子類都有實際存儲數(shù)據(jù)的作用,而代理模型繼承父類的數(shù)據(jù)而不存儲實際的數(shù)據(jù),代理模型繼承通過在子類的Meta類中定義proxy=True屬性來實現(xiàn):
from django.db import models class Mement(models. Model): id = models.AutoField(default=0) content = models.CharField(max_length=100) user_name = models.CharField(max_length=80) pub_date = models.DateField() class OrderByMoment(Moment): #使用代理模型繼承的原因是子類中新的特性不會影響父類的模型及其已有代碼的行為 class Meta: proxy = True ordering = ['-pub_date'] #Moment根據(jù)pub_date倒序排列
Moment類用于存儲數(shù)據(jù),OrderByMoment類用于管理根據(jù)pub_date倒序排列的Moment
希望本文所述對大家基于Django框架的Python程序設(shè)計有所幫助。
相關(guān)文章
Python操作Redis數(shù)據(jù)庫的超詳細教程
大家應(yīng)該都知道redis是一個基于內(nèi)存的高效的鍵值型非關(guān)系數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Python操作Redis的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06Python Pandas學習之數(shù)據(jù)離散化與合并詳解
Pandas是python的一個數(shù)據(jù)分析包,該工具是為解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的。本文將通過示例詳細為大家介紹一下Pandas的數(shù)據(jù)離散化與合并,需要的可以參考一下2022-02-02Python調(diào)用API接口實現(xiàn)人臉識別
本文主要介紹了Python調(diào)用API接口實現(xiàn)人臉識別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02Python實現(xiàn)學生管理系統(tǒng)的完整代碼(面向?qū)ο?
這篇文章主要介紹了Python實現(xiàn)學生管理系統(tǒng)的完整代碼(面向?qū)ο?,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04