gpt4 book ai didi

marshmallow - 反序列化多对多 PUT 操作的 id 列表

转载 作者:行者123 更新时间:2023-12-04 10:37:21 26 4
gpt4 key购买 nike

我有一个多对多关系的用户和角色模型

class User(BaseModel, TimestampableMixin):

username = Column(String(MEDIUM_STRING_LENGTH), nullable=False, unique=True)

roles = relationship('Role', secondary='user_roles', back_populates='users')

class Role(BaseModel, TimestampableMixin):

label = Column(String(MEDIUM_STRING_LENGTH), nullable=False, unique=True)

users = relationship('User', secondary='user_roles', back_populates='roles')

class UserRole(BaseModel, TimestampableMixin):

user_id = Column(ForeignKey('users.id', ondelete=CASCADE, onupdate=CASCADE), nullable=False, index=True)
role_id = Column(ForeignKey('roles.id', onupdate=CASCADE), nullable=False, index=True)

然后我为用户定义了模式来嵌套角色。
class RoleSchema(BaseSchema):
class Meta:
model = models.Role

class UserSchema(BaseSchema):
class Meta:
model = models.User

roles = fields.Nested('RoleSchema', many=True, exclude=['users'])

对于序列化,在用户 GET 请求中包含角色对象列表的情况下,这非常有用。还可以使用嵌入在请求中的新角色对象发布用户。我无法弄清楚的是如何发布/放置现有角色 ID 的列表,而不是创建新对象。
例如,此请求有效:
{
"username": "testuser12",
"roles": [
{
"label": "newrole"
}
]
}

回复:
{
"createdTime": "2020-02-06T19:13:29Z",
"id": 4,
"modifiedTime": "2020-02-06T19:13:29Z",
"roles": [
{
"createdTime": "2020-02-06T19:13:29Z",
"id": 2,
"label": "newrole",
"modifiedTime": "2020-02-06T19:13:29Z"
}
],
"username": "testuser12"
}

但是这些请求都不起作用:
{
"username": "testuser13",
"roles": [
1
]
}

{
"username": "testuser13",
"roles": [
{
"id": 1
}
]
}

我得到这个回应:
{
"errors": {
"error": [
"Unprocessable Entity"
],
"message": [
"The request was well-formed but was unable to be followed due to semantic errors."
]
}
}

我可以说我在模式中遗漏了一些能够摄取 id 而不是对象的东西,我怀疑我需要使用 dump_only/load_only 并且可能是一个单独的 PUT 模式。但是,我无法在网上的任何地方找到此用例的示例。

提及我正在使用 flask-smorest 也可能会有所帮助。用于请求验证和架构参数摄取。
@B_API.route('/user/<user_id>')
class UserByIdResource(MethodView):

@B_API.response(schemas.UserSchema)
def get(self, user_id):
"""
Get a single user by id
"""
return models.User.query.get(user_id)

@B_API.arguments(schemas.UserSchema)
@B_API.response(schemas.UserSchema)
def put(self, updated_user, user_id):
"""
Update fields of an existing user
"""
models.User.query.get_or_404(user_id, description=f'User with id {user_id} not found')
user = updated_user.update_with_db(user_id)
return user

update_with_db 看起来像:
    def update_with_db(self, id: int):
self.id = id
DB.session.merge(self)
DB.session.commit()
return self.query.get(id)

感谢您的帮助。

最佳答案

我最终通过创建一个单独的 PUT 模式并使用 Pluck 作为角色 ID 来解决这个问题。

class UserSchema(BaseModelSchema):
class Meta:
model = models.User

roles = fields.Nested('RoleSchema', many=True, exclude=['users'])


class UserPutSchema(BaseModelSchema):
class Meta:
model = models.User

roles = fields.Pluck('RoleSchema', 'id', many=True)

这些架构使用以下资源实现,采用 PUT 架构进行输入验证,并返回标准用户架构以符合 GET 和 POST 响应。这似乎工作得很好,唯一的缺点是必须定义一个单独的模式。
    @B_API.arguments(schemas.UserPutSchema(partial=True))
@B_API.response(schemas.UserSchema)
def put(self, updated_user, user_id):
"""
Update fields of an existing user
"""
get_by_id(models.User, user_id)
user = updated_user.update_with_db(user_id)
return user

我现在可以发出 PUT 请求,例如
PUT /user/1
{
"roles": [
1,
2
]
}

并得到类似的回应
{
"createdTime": "2020-02-20T01:33:42Z",
"historys": [],
"id": 1,
"modifiedTime": "2020-02-20T01:33:42Z",
"roles": [
{
"createdTime": "2020-02-19T23:43:06Z",
"description": null,
"historys": [],
"id": 1,
"key": "TEST_ROLE_1",
"label": "role 1",
"modifiedTime": "2020-02-19T23:43:06Z"
},
{
"createdTime": "2020-02-19T23:43:06Z",
"description": null,
"historys": [],
"id": 2,
"key": "TEST_ROLE_2",
"label": "role 2",
"modifiedTime": "2020-02-19T23:43:06Z"
}
],
"username": "testuser1"
}

这就是我的目标。

关于marshmallow - 反序列化多对多 PUT 操作的 id 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60102126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com