【问题标题】:Flask, marshmallow - problem with nested field烧瓶,棉花糖 - 嵌套字段的问题
【发布时间】:2020-02-09 02:39:08
【问题描述】:

我有 2 个具有“一对一”关系的表。

当我从一个表中获取数据时,我想包含相关表中的一个字段,但没有“点表示法”。

什么有效:

class UserEntitySchema(db_schema.Schema):
    class Meta:
        fields = ('id', 'username', 'email', 'confirmed', 'created', 'enabled', 'account.status')

我希望“account.status”只是“status”,但我不知道如何获得它。

我按照@marke 的建议尝试了 Pluck,但没有任何结果。这里有什么问题吗?

class AccountEntitySchema(db_schema.Schema):
    current_status = fields.Str()
    class Meta:
        fields =('current_status',)

class UserEntitySchema(db_schema.Schema):
    status = fields.Pluck(AccountSchema, 'current_status')
    class Meta:
        fields = ('id', 'username', 'email', 'status')

我更新的有效解决方案(感谢@marke!):

class AccountEntitySchema(db_schema.Schema):
    class Meta:
        fields =('current_status',)

class UserEntitySchema(db_schema.Schema):
    account = fields.Pluck(AccountSchema, 'current_status', data_key='status') #<==== data_key will replace the existing field
    class Meta:
        # Fields to expose
        fields = ('id', 'username', 'email', 'account')

【问题讨论】:

  • 使用 pluck 字段怎么样:marshmallow.readthedocs.io/en/stable/…
  • @marke 感谢您的建议。还没有结果:(更新了我的帖子,如果我做错了什么,你能看看吗?
  • @marke,非常感谢,我终于成功了! (如果您将评论作为答案,则会将其作为解决方案进行检查)。
  • 很高兴听到这个消息;P

标签: python flask-sqlalchemy marshmallow flask-marshmallow


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2020-09-13
    • 2020-09-23
    • 2021-02-12
    相关资源
    最近更新 更多