我在数据库中有一个模型来存储全局应用程序设置。设置可以有不同的类型,所以我制作了一个具有多态身份的 data_type 广告鉴别器。
class MiscSetting(db.Model):
__tablename__ = "settings_misc"
__table_args__ = (db.UniqueConstraint("county", "name", name="uc_county_name"),)
id = db.Column(db.Integer, primary_key=True)
county = db.Column(db.String, nullable=True)
name = db.Column(db.String(length=50))
data_type = db.Column(db.String(50))
__mapper_args__ = {
'polymorphic_on': data_type,
'with_polymorphic': '*'
}
class StringSetting(MiscSetting):
__tablename__ = 'settings_string'
id = db.Column(db.Integer, db.ForeignKey('settings_misc.id'), primary_key=True)
value = db.Column(db.String)
__mapper_args__ = {
'polymorphic_identity': 'string'
}
class IntegerSetting(MiscSetting):
__tablename__ = 'settings_integer'
id = db.Column(db.Integer, db.ForeignKey('settings_misc.id'), primary_key=True)
value = db.Column(db.Integer)
__mapper_args__ = {
'polymorphic_identity': 'integer'
}
当我尝试转储我收到的所有对象时,没有找到“价值”。
settings = db.session.query(MiscSetting).all()
dump = json.dumps(MiscSettingSchema().dump(settings, many=True))
class MiscSettingSchema(BaseSchema):
class Meta:
model = MiscSetting
问题是如何将价值包含到棉花糖模式中?