在我的Flask-SQLAlchemy应用程序中,我想向每个模型/表中添加一些字段(创建(由| on),更改(由|| on))
我的代码现在
from .. import db
class Brand(db.Model):
__tablename__ = 'md_brands'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True, nullable=False)
def __repr__(self):
return u'<Brand {}>'.format(self.name)
我不确定使用Mixins还是以某种方式扩展基本db.Model更好(或者是否有更好的方法来做到这一点)。
向我的所有模型添加此类字段(创建(by | on),更改(by | on))的最佳方法是什么(为什么)?
两者几乎相同。这是我使用的Mixin
class ModelMixin(object):
def __repr__(self):
return unicode(self.__dict__)
@property
def public_view(self):
"""return dict without private fields like password"""
return model_to_dict(self, self.__class__)
进而
class User(db.Model, ModelMixin):
""" attributes with _ are not exposed with public_view """
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句