Flask-SQLAlchemy设置关系默认值 [英] Flask-SQLAlchemy set relationship default value
问题描述
我有一个Flask制作的应用程序,我使用Flask-admin和Flask-SQLAlchemy进行数据库管理. 在我的应用中,有三个角色,即:
I have an app making by Flask and for database management I using Flask-admin and Flask-SQLAlchemy. And in my app there has three role, which is:
- 管理员,
- 学校
- 父母
- admin,
- school
- parent
这是我的表上的代码段:
here is the snipet of code on my table:
roles_users = db.Table(
'roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))
)
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
class School(User):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
class Parent(User):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
school_id = db.Column(db.Integer, db.ForeignKey('school.id'))
代码已经可以正常工作了.
The code already works fine.
- 管理员可以创建学校帐户,并将用户的角色设置为学校.
- 和具有学校角色的用户可以注册父母帐户.
- admin can make the school account and set the role for the user to school.
- and a user that has a school role can make parents account.
我可以使用Flask-admin界面为所有用户手动设置角色,也可以使用具有 admin 角色的用户进行设置.
I can set the role for all of the users manually with Flask-admin interface, I can make it with a user that has admin role.
但是,对于具有 school 角色的用户,我没有权限设置 admin 那样的角色.
But, for the user that has school role, I don't give access to set the role like admin does.
我想要的是当具有学校角色的用户注册帐户时,它会自动将该角色设置为父母.
What I want is when the user that has school role make an account, it automatically set the role to parent.
在我的ModelView中,我尝试使用以下代码将其插入:
In my ModelView, I try to insert it with this code:
class ParentModelView(sqla.ModelView):
def on_model_change(self, form, model, is_created):
if is_created:
model.school_id = current_user.id
model.roles = 'parent'
然后出现此错误:
TypeError: Incompatible collection type: str is not list-like
然后我尝试用列表包装它:
and then I try to wraping it with list:
def on_model_change(self, form, model, is_created):
if is_created:
model.school_id = current_user.id
model.roles = ['parent']
然后错误是这样的:
AttributeError: 'str' object has no attribute '_sa_instance_state'
当我尝试在我的关系"列上设置默认值时:
And when I try to set the default value on my relationship column :
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'),
default='parent') # or default=3 (which is '3' is the parent id in roles_user association table.
然后出现此错误:
TypeError: relationship() got an unexpected keyword argument 'default'
所以,我要说的是在定义角色用户的关系列上设置默认值.
So, the point what I want is to set a default value on my relationship column that defines roles users.
任何帮助将不胜感激.
推荐答案
在您的代码中,当您应该分配Role
实例列表时,您试图分配字符串'parent'.
In your code you are trying to assign the string 'parent' when you should be assigning a list of Role
instances.
在on_model_change
方法中,您需要从数据库中获取名称为"Parent"的角色,然后将其作为列表分配给角色关系.
In your on_model_change
method you need to fetch the role that has name 'Parent' from the database and then assign it to the roles relationship as a list.
示例(未经测试)
def on_model_change(self, form, model, is_created):
if is_created:
# find the role with name 'Parent'
_parent_role = Role.query.filter(Role.name == 'Parent').first()
if _parent_role:
model.school_id = current_user.id
model.roles = [_parent_role]
这篇关于Flask-SQLAlchemy设置关系默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!