您好,登錄后才能下訂單哦!
Django和Flask都是流行的Python Web框架,它們都提供了ORM(對(duì)象關(guān)系映射)功能,用于處理數(shù)據(jù)庫操作。在這兩個(gè)框架中,模型繼承是一種常見的做法,用于重用代碼和保持?jǐn)?shù)據(jù)庫結(jié)構(gòu)的清晰。下面分別介紹Django和Flask ORM中的模型繼承。
Django支持多種類型的模型繼承,包括單表繼承、多表繼承和代理模型。
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Category(BaseModel):
name = models.CharField(max_length=100)
class Product(BaseModel):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Category(BaseModel):
name = models.CharField(max_length=100)
class Product(BaseModel):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Category(BaseModel):
name = models.CharField(max_length=100)
class Product(BaseModel):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
class ProductProxy(Product):
class Meta:
proxy = True
Flask使用SQLAlchemy作為ORM庫,支持多種類型的繼承,包括單表繼承、多表繼承和混合繼承。
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class BaseModel(db.Model):
__abstract__ = True
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class Category(BaseModel):
name = db.Column(db.String(100))
class Product(BaseModel):
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
price = db.Column(db.Float)
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class BaseModel(db.Model):
__abstract__ = True
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class Category(BaseModel):
name = db.Column(db.String(100))
class Product(BaseModel):
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
price = db.Column(db.Float)
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class BaseModel(db.Model):
__abstract__ = True
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())
updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp())
class Category(BaseModel):
name = db.Column(db.String(100))
class Product(BaseModel):
__table_args__ = (db.ForeignKey('category.id'),)
category_id = db.Column(db.Integer)
price = db.Column(db.Float)
總結(jié):Django和Flask都支持多種類型的模型繼承,具體實(shí)現(xiàn)方式略有不同。在實(shí)際項(xiàng)目中,可以根據(jù)需求和場景選擇合適的繼承方式。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。