【问题标题】:Nested Marshmallow and Sqlalchemy include children嵌套 Marshmallow 和 Sqlalchemy 包括子级
【发布时间】:2019-06-25 07:50:25
【问题描述】:

我有一个与 2 个表相关的数据透视表 (postgres)。使用 flask-marshmallow 和 sqlalchemy 我想从他们的任何棉花糖模式中从两个表中获取数据。例如:Table1Schema().dump(table1_object).first: 并获取与 Table2 的数据连接的 Table1 记录(Many=True): 以下是我当前的代码:

仅供参考:我是烧瓶和 ORM 世界的新手:

我的模特:

class Permission(db.Model):
    __tablename__ = 'permission'
    permission_id = db.Column(db.Integer, primary_key=True)
    object = db.Column(db.String(70), nullable=False)

    def __init__(self, object):
        self.object = object


class Role(db.Model):
    __tablename__ = 'role'
    role_id = db.Column(db.Integer, primary_key = True)
    role_name = db.Column(db.String(70), nullable=False)
    role_permissions = db.relationship("RolePermission",backref='Role', lazy='dynamic')

    def __init__(self,role_name):
        self.role_name = role_name



class RolePermission(db.Model):
    __tablename__ = 'role_permission'
    role_permission_id = db.Column(db.Integer, primary_key=True)
    role_id = db.Column(db.Integer, db.ForeignKey('role.role_id', ondelete='CASCADE'), nullable =False)
    permission_id = db.Column(db.Integer, db.ForeignKey('permission.permission_id', ondelete='CASCADE'), nullable =False)

    def __init__(self,role_id,permission_id):
        self.role_id = role_id
        self.permission_id = permission_id

还有我的架构:

class RoleSchema(ma.Schema):
    role_id = fields.Integer(dump_only=True)
    role_name =  fields.String(required=True, validate=validate.Length(1))
    permissions = fields.Nested('RolePermissionSchema', many=True, only=('permission',))

    class Meta:
        model = Role
        fields = ('role_id', 'role_name', 'permissions')


class RolePermissionSchema(ma.Schema):
    role_permission_id = fields.Integer(dump_only=True)
    role_id = fields.Integer(required=True)
    permission_id = fields.Integer(required=True)
    role = fields.Nested('ROleSchema', many=False, only=('role_id', 'role_name',))
    permission = fields.Nested('PermissionSchema', many=False, only=('object', 'action',))




class PermissionSchema(ma.Schema):
    permission_id = fields.Integer(dump_only=True)
    object = fields.String(required=True, validate=validate.Length(1))
    role_permissions = fields.Nested('RolePermissionSchema', many=True, only=('role_permission_id', 'role_id',))
    class Meta:
        fields = ('object','action','role_permissions',)

序列化器:

 role = Role.query.filter_by(role_name=data['role_name']).filter_by(status=data['status']).first()
role_schema.dump(role).data

我想用权限[] 对象打印一个 Role o 对象。 然而,从上面我能得到的只是角色内容。 以下是输出:

{
        "role_name": "user",
        "status": true,
        "role_id": 4
    }

我如何得到这样的东西:

{
    "role_name": "user",
    "status": true,
    "role_id": 4,
    "permissions":[{
         "object":"user",
         "action":"create"},
          ]
}

【问题讨论】:

  • 这是您的实际代码吗?例如,fields.Nested('ROleSchema', many=False, only=('role_id', 'role_name',)) 具有 ROleSchema 而不是 RoleSchema。然后返回 Role 而不是 role
  • @J.J.Hakala 是的。我改变了这两个,仍然没有得到数据。
  • 另外,RoleSchema 中的permissionsRole 中的role_permissions 不对应
  • @J.J.Hakala,非常感谢。您可以将 2 个 cmets 组合在一起作为我接受的答案。还要把 Model 和 Schema 字段名的概念放在下一个可能需要这个的开发者身上。

标签: python flask sqlalchemy flask-restful marshmallow


【解决方案1】:

存在一些命名问题,此处由注释行 # 指出。

class Role(db.Model):
   __tablename__ = 'role'
   role_id = db.Column(db.Integer, primary_key = True)
   role_name = db.Column(db.String(70), nullable=False)
   # This backref should probably be named role
   role_permissions = db.relationship("RolePermission",backref='Role', lazy='dynamic')

class RoleSchema(ma.Schema):
    role_id = fields.Integer(dump_only=True)
    role_name =  fields.String(required=True, validate=validate.Length(1))
    # This does not correspond to role_permissions in class Role
    permissions = fields.Nested('RolePermissionSchema', many=True, only=('permission',))

    class Meta:
        model = Role
        fields = ('role_id', 'role_name', 'permissions')

class RolePermissionSchema(ma.Schema):
   role_permission_id = fields.Integer(dump_only=True)
   role_id = fields.Integer(required=True)
   permission_id = fields.Integer(required=True)
   # Should be RoleSchema
   role = fields.Nested('ROleSchema', many=False, only=('role_id', 'role_name',))
   permission = fields.Nested('PermissionSchema', many=False, only=('object', 'action',))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多