简体   繁体   English

Flask-SQLAlchemy设置关系默认值

[英]Flask-SQLAlchemy set relationship default value

I have an app making by Flask and for database management I using Flask-admin and Flask-SQLAlchemy. 我有一个由Flask开发的应用程序,用于数据库管理,我使用Flask-admin和Flask-SQLAlchemy。 And in my app there has three role, which is: 在我的应用中,有三个角色,分别是:

  1. admin , 管理员
  2. school 学校
  3. 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. 具有学校角色的用户可以注册父母帐户。

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. 我可以使用Flask-admin界面为所有用户手动设置角色,也可以使用具有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 . 我想要的是具有学校角色的用户注册帐户后,它会自动将角色设置为parent

In my ModelView, I try to insert it with this code: 在我的ModelView中,我尝试使用以下代码将其插入:

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'

and then I got this error: 然后我得到这个错误:

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']

then the error be like this: 那么错误是这样的:

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.

and then I got this error: 然后我得到这个错误:

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. 因此,我想要的是在定义角色用户的关系列上设置默认值。

Any help will be much appreciated. 任何帮助都感激不尽。

In your code you are trying to assign the string 'parent' when you should be assigning a list of Role instances. 在您的代码中,当您应该分配Role实例列表时,您尝试分配字符串“ 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. on_model_change方法中,您需要从数据库中获取名称为“ Parent”的角色,然后将其作为列表分配给角色关系。

Example (untested) 示例 (未经测试)

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM