1
ベースクラスを使用するのではなく、Mixin
を使用したい、私はFLASKでMixinsを使用することに関する多くの文書を見つけられませんでした。私はこの抽象的な基本クラスの代わりにミックスインをどのように実装することができますか?ミックスインとSQLAlchemyの上FlaskにMIXINはどのように実装できますか?
class Base(db.Model):
"""
Base class for models.
Define the base class for the models so others can inherit from it.
"""
__abstract__ = True
def save(self):
"""
Save to database.
Save instance of the object to database and commit.
"""
db.session.add(self)
db.session.commit()
def delete(self):
"""
Delete from database.
Deletes instance of an object from database
"""
db.session.delete(self)
db.session.commit()
class User(Base):
"""
Set up the User model.
Set up the properties of the User object and the table name too.
"""
__tablename__ = 'users'
user_id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(32), unique=True, index=True,
nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
date_created = db.Column(
db.DateTime, default=datetime.now(), nullable=False)
date_modified = db.Column(
db.DateTime, default=datetime.now(),
onupdate=datetime.now(), nullable=False)
def hash_password(self, password):
"""
Hash user password.
Passwords shouldn't be stored as string so we hash them.
"""
self.password_hash = generate_password_hash(password)
http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-the y有用な – stamaimer