学习Flask-SQLAlchemy,求教如何更改用户的角色。如果现在创建了角色 Administrator、User、Moderator,id依次为1、2、3。 用户 L, id为1,L的角色为User。 请问怎样在python shell中更改用户的角色???
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
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.
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
When using Python to call the MySQL driver, it is better to change it directly in MySQL.
Suppose the model is like this:
According to what you said, suppose there is user L(id=1, username='xxx', role_id=2)
Then the user role becomes Administrator at this time.
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
When using Python to call the MySQL driver, it is better to change it directly in MySQL.