python - flask--数据库 如何改变一个用户的角色?
高洛峰
高洛峰 2017-04-18 10:25:59
0
3
470

学习Flask-SQLAlchemy,求教如何更改用户的角色。
如果现在创建了角色 Administrator、User、Moderator,id依次为1、2、3。 用户 L, id为1,L的角色为User。 请问怎样在python shell中更改用户的角色???

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
左手右手慢动作

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template