使用SQLAlchemy操作MYSQL黑科技之 屬性訂制
阿新 • • 發佈:2017-09-26
relation check kref sql 黑科技 ack ada generate user
1 class Role(UserMixin, db.Model): 2 __tablename__ = ‘Role‘ 3 id = db.Column(db.Integer, primary_key=True) 4 username = db.Column(db.String(64), nullable=True) 5 role = db.Column(db.String(64), nullable=True) 6 password = db.Column(db.String(64), nullable=True) 7 password_hash = db.Column(db.String(128), nullable=True)8 9 role_art = db.relationship(‘Article‘, backref=‘roleart‘) 10 role_com = db.relationship(‘Comment‘, backref=‘rolecom‘) 11 12 @property 13 def password(self): 14 raise AttributeError(‘password is not readable attribute‘) #沒有可讀的明文密碼屬性 15 16 @password.setter #設置 哈希後的密碼值17 def password(self, password): 18 self.password_hash = generate_password_hash(password) 19 20 def verify_password(self, password): 21 return check_password_hash(self.password_hash, password)
使用SQLAlchemy操作MYSQL黑科技之 屬性訂制