Flask-restplus fields.Nested() 与原始 Dict (不是模型)

Dov*_*ovi 6 python model flask flask-restplus

剧透警报:我发布了我的解决方案作为此问题的答案

我正在使用flastk-resptlus创建 API。我必须提供特定结构的数据,但我无法获取该数据,请参阅下面的示例:

我需要得到的是这个结构:

{
    "metadata": {
        "files": [] 
    },
    "result" : {
        "data": [
                {
                 "user_id": 1,
                  "user_name": "user_1",
                  "user_role": "editor"
                },
                {
                  "user_id": 2
                  "user_name": "user_2",
                  "user_role": "editor"
                },
                {
                  "user_id": 3,
                  "user_name": "user_3",
                  "user_role": "curator"
                }
            ]
    }
}
Run Code Online (Sandbox Code Playgroud)

"result" : { "data": []}但问题来了,如果不让“数据”本身成为模型,我就无法获得结构。

到目前为止我尝试做的事情(但没有成功)

# define metadata model
metadata_model = api.model('MetadataModel', {
          "files": fields.List(fields.String(required=False, description='')),
}
# define user model 
user_model = api.model('UserModel', {
          "user_id": fields.Integer(required=True, description=''),
          "user_name": fields.String(required=True, description=''),
          "user_role": fields.String(required=False, description='')
}

# here is where I have the problems
user_list_response =  api.model('ListUserResponse', {
            'metadata': fields.Nested(metadata_model),
            'result' :  {"data" : fields.List(fields.Nested(user_model))}
             })
Run Code Online (Sandbox Code Playgroud)

抱怨无法从中获取“模式” "data"(因为不是定义的模型),但我不想成为新的 api 模型,只想附加一个名为“数据”的键。有什么建议么?

我尝试过并且有效,但这不是我想要的(因为我错过了“数据”):

user_list_response =  api.model('ListUserResponse', {
            'metadata': fields.Nested(metadata_model),
            'result' :  fields.List(fields.Nested(user_model))
            })
Run Code Online (Sandbox Code Playgroud)

我不想data成为一个模型,因为api的常见结构如下:

{
    "metadata": {
        "files": [] 
    },
    "result" : {
        "data": [
                <list of objects> # here must be listed the single model
            ]
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,<list of objects>可以是用户、地址、工作等等......所以我想制作一个“通用结构”,然后我可以在其中注入特定模型(UserModel、AddressModel、JobModel 等),而无需为data每个模型创建特殊模型。

Mol*_*ris 6

一种可能的方法是使用fields.Raw它返回您传递的任何可序列化对象。然后,您可以定义第二个函数,它创建您的结果并使用marshal. marshal根据模型转换数据并接受名为 的附加参数envelopeenvelope用给定的键包围您的建模数据并完成任务。

from flask import Flask
from flask_restplus import Api, fields, Resource, marshal

app = Flask(__name__)
api = Api()
api.init_app(app)

metadata_model = api.model("metadata", {
    'file': fields.String()
})

user_model = api.model('UserModel', {
          "user_id": fields.Integer(required=True, description=''),
          "user_name": fields.String(required=True, description=''),
          "user_role": fields.String(required=False, description='')
})

response_model = api.model("Result", {
    'metadata': fields.List(fields.Nested(metadata_model)),
    'result': fields.Raw()
})


@api.route("/test")
class ApiView(Resource):

    @api.marshal_with(response_model)
    def get(self):

        data = {'metadata': {},
                'result': self.get_user()}
        return data


    def get_user(self):
        # Access database and get data
        user_data = [{'user_id': 1, 'user_name': 'John', 'user_role': 'editor'},
                     {'user_id': 2, 'user_name': 'Sue', 'user_role': 'curator'}]

        # The kwarg envelope does the trick
        return marshal(user_data, user_model, envelope='data')


app.run(host='0.0.0.0', debug=True)
Run Code Online (Sandbox Code Playgroud)

回复


Dov*_*ovi 3

我的解决方案解决了我所有的问题:

我创建一个新的 List fields 类(它主要是从 fields.List 复制的),然后我只需调整输出格式和模式以获得“数据”作为键:

class ListData(fields.Raw):
    '''
    Field for marshalling lists of other fields.

    See :ref:`list-field` for more information.

    :param cls_or_instance: The field type the list will contain.

    This is a modified version of fields.List Class in order to get 'data' as key envelope
    '''
    def __init__(self, cls_or_instance, **kwargs):
        self.min_items = kwargs.pop('min_items', None)
        self.max_items = kwargs.pop('max_items', None)
        self.unique = kwargs.pop('unique', None)
        super(ListData, self).__init__(**kwargs)
        error_msg = 'The type of the list elements must be a subclass of fields.Raw'
        if isinstance(cls_or_instance, type):
            if not issubclass(cls_or_instance, fields.Raw):
                raise MarshallingError(error_msg)
            self.container = cls_or_instance()
        else:
            if not isinstance(cls_or_instance, fields.Raw):
                raise MarshallingError(error_msg)
            self.container = cls_or_instance
    def format(self, value):

        if isinstance(value, set):
            value = list(value)

        is_nested = isinstance(self.container, fields.Nested) or type(self.container) is fields.Raw

        def is_attr(val):
            return self.container.attribute and hasattr(val, self.container.attribute)

        # Put 'data' as key before the list, and return the dict
        return {'data': [
            self.container.output(idx,
                val if (isinstance(val, dict) or is_attr(val)) and not is_nested else value)
            for idx, val in enumerate(value)
        ]}

    def output(self, key, data, ordered=False, **kwargs):
        value = fields.get_value(key if self.attribute is None else self.attribute, data)
        if fields.is_indexable_but_not_string(value) and not isinstance(value, dict):
            return self.format(value)

        if value is None:
            return self._v('default')
        return [marshal(value, self.container.nested)]

    def schema(self):
        schema = super(ListData, self).schema()
        schema.update(minItems=self._v('min_items'),
                      maxItems=self._v('max_items'),
                      uniqueItems=self._v('unique'))

        # work around to get the documentation as I want
        schema['type'] = 'object'
        schema['properties'] = {}
        schema['properties']['data'] = {}
        schema['properties']['data']['type'] = 'array'
        schema['properties']['data']['items'] = self.container.__schema__

        return schema
Run Code Online (Sandbox Code Playgroud)