学习Flask-SQLAlchemy,求教如何更改用户的角色。
如果现在创建了角色 Administrator、User、Moderator,id依次为1、2、3。 用户 L, id为1,L的角色为User。 请问怎样在python shell中更改用户的角色???
ringa_lee2017-04-18 10:27:59
Suppose the model is like this:
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(), unique=True)
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
According to what you said, suppose there is user L(id=1, username='xxx', role_id=2)
>>> u=User.query.get_or_404(1)#忽略import语句
>>> u.role_id=1
>>> db.session.add(u)
>>> db.session.commit()
>>> u.role_id
1
Then the user role becomes Administrator at this time.
高洛峰2017-04-18 10:27:59
This kind of thing obviously needs to be changed by adjusting the interface in Flask. You have to use Python Shell to change it. It is better to change it directly in mysql
PHP中文网2017-04-18 10:27:59
When using Python to call the MySQL driver, it is better to change it directly in MySQL.