【问题标题】:How can I load only a foreign key into Nested fields using Marshmallow / SQLAlchemy如何使用 Marshmallow / SQLAlchemy 仅将外键加载到嵌套字段中
【发布时间】:2021-04-19 23:06:32
【问题描述】:

我对 Flask-Marshmallow / Flask-SQLAlchemy 非常陌生,我正在尝试使用 flask 和 mysql 设置我自己的 REST API。

这是我要发布的有效负载。我希望能够只发送 ID 并排除所有其他字段:

{
    "code": "FG034",
    "product_name": "test test2",
    "description": "Description",
    "init_date": "2021-01-10",
    "init_by": {
        "id": "27bb9e1acad247618fb2c3e016ae841c"
    }
}

这是我发送该有效负载时遇到的错误:

ERROR in utils: (pymysql.err.IntegrityError) (1048, "Column 'full_name' cannot be null")
[SQL: INSERT INTO user (id, full_name, initials, hashed_password, is_active) VALUES (%(id)s, %(full_name)s, %(initials)s, %(hashed_password)s, %(is_active)s)]
[parameters: {'id': '27bb9e1acad247618fb2c3e016ae841c', 'full_name': None, 'initials': None, 'hashed_password': None, 'is_active': 1}]
(Background on this error at: http://sqlalche.me/e/13/gkpj)

这很奇怪,因为我没有尝试将 anything 插入用户表...

当我将所有字段放入其中时,会按预期添加:

{
    "code": "FG034",
    "product_name": "test test2",
    "description": "Description",
    "init_date": "2021-01-10",
    "init_by": {
        "id": "27bb9e1acad247618fb2c3e016ae841c",
        "initials": "EE",
        "email": "example@gmail.com",
        "full_name": "Example Exampleton",
        "is_active": true
    }
}

这是添加的 FinishedGood 结果的 GET(这是我想要的):

{
    "code": "FG034",
    "init_date": "2021-01-10",
    "description": "Description",
    "id": 3,
    "is_active": true,
    "init_by": {
        "email": "example@gmail.com",
        "id": "27bb9e1acad247618fb2c3e016ae841c",
        "is_active": true,
        "full_name": "Example Exampleton",
        "initials": "EE"
    },
    "product_name": "test test2"
}

也许我完全用错了,但谁能告诉我:

  1. 为什么要插入用户表?我想要的只是加载到 init_by_id 的主键
  2. 有没有办法像我在开头显示的那样仅使用 ID 发布有效负载?

这是我的类和架构:

class User(db.Model):
    id = db.Column(db.CHAR(32), primary_key=True, default=uuid.uuid4().hex)
    email = db.Column(db.String(255), primary_key=True)
    full_name = db.Column(db.String(255), nullable=False)
    initials = db.Column(db.String(3), unique=True, nullable=False)
    hashed_password = db.Column(db.Text, nullable=False)
    is_active = db.Column(db.Boolean, default=True)

class UserSchema(ma.SQLAlchemySchema):
    class Meta:
        model = User
        load_instance = True
        sqla_session = db.session

    id = ma.auto_field()
    email = ma.auto_field(required=False)
    full_name = ma.auto_field(required=False)
    initials = ma.auto_field(required=False)
    is_active = ma.auto_field(required=False)

class FinishedGood(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.String(255), unique=True, nullable=False)
    product_name = db.Column(db.String(255), nullable=False)
    description = db.Column(db.Text)
    init_date = db.Column(db.Date, nullable=False)
    init_by_id = db.Column(db.CHAR(32), db.ForeignKey(User.id), nullable=False)
    init_by = db.relationship(User)
    is_active = db.Column(db.Boolean, default=True)

class FinishedGoodSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = FinishedGood
        load_instance = True
        sqla_session = db.session

    init_by = ma.Nested(UserSchema)

最后是我的 post 方法:

def post_finished_good():
    data = request.get_json(force=True)
    fg = FinishedGoodSchema().load(data)
    db.session.add(fg)
    db.session.commit()
    return FinishedGoodSchema().jsonify(fg), 201

【问题讨论】:

  • 好问题,我的项目中也有类似的问题,只是多对多的关系。出于安全原因,不能允许创建新 A 对象的 POST 操作添加或更新 B 对象。我只想传入一个B 对象ID 列表以与新的A 关联。希望有人可以提供解决方案。

标签: python json flask-sqlalchemy flask-marshmallow


【解决方案1】:

TLDR:将 init_by 字段更改为 init_by = autofield()。然后将init_by 成员加载为:"init_by": "27bb9e1acad247618fb2c3e016ae841c"。 使用init_by = Nested(UserSchema) 为序列化创建单独的架构以生成所需的输出。

class User(db.Model):
    # columns omitted for brevity

class UserSchema(ma.SQLAlchemySchema):
    # fields omitted for brevity

class FinishedGood(db.Model):
    init_by = db.relationship(User)
    # columns omitted for brevity

class FinishedGoodSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = FinishedGood
        load_instance = True
        sqla_session = db.session

        # default is False
        load_relationships = True

    # or
    init_by = autofield()

    # remove this: nested fields seem to apply only for serialization and for loading a new instance 
    # init_by = ma.Nested(UserSchema)

有效载荷:

{
    "code": "FG034",
    "product_name": "test test2",
    "description": "Description",
    "init_date": "2021-01-10",
    "init_by": "27bb9e1acad247618fb2c3e016ae841c"
}

我尚未对此进行测试,但请随时发表评论以进行澄清。

【讨论】:

    【解决方案2】:

    我也是 Marshmallow 的新手,所以这可能不是最佳解决方案,但我设法使用 @pre_load 装饰器找到了解决方法:

    @pre_load
    def pre_load_process(self, data, **kwargs):
      id = data.get('id')
      if id is None:
        return data
    
      user = User.query.filter(User.id == id).first()
      if user is None:
        return data
    
      merged_data = UserSchema().dump(user)
      merged_data.update(data)
    
      return merged_data
    
    

    本质上,将此方法添加到您的UserSchema 类使其能够从id 反序列化,仅在您的示例有效负载中。如果包含其他字段以及id,它们将覆盖从数据库中提取的值。

    如果您需要防止通过FinishedGood 端点更新User(我的项目也是如此),那么FinishedGoodSchema 还需要一个@pre_load 装饰方法来剥离除id 之外的所有内容来自嵌套的init_by。

    这似乎不是一个特别奇特的用例,所以我很惊讶在网上找到相关示例是多么困难 - 我发现的只是你三个月大的问题没有答案......就像我说的,我也是使用这个库的初学者,希望能从棉花糖专家那里得到一些背景信息。当这种表面上简单的事情实施起来却令人头疼时,这让我担心我的整个方法被误解了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-10
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 2019-08-10
      • 2019-06-25
      相关资源
      最近更新 更多