flask-restplus fields.Nested() 与原始字典(非模型)

flask-restplus fields.Nested() with raw Dict (not model)

剧透警告:我发布了我的解决方案作为对这个问题的回答

我正在使用 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"
                }
            ]
    }
}

但问题来了,如果不使 "data" 成为模型本身,我就无法获得 "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))}
             })

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

这个我试过了并且有效,但不是我想要的(因为我错过了"data"):

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

我不希望data成为模型,因为api的通用结构如下:

{
    "metadata": {
        "files": [] 
    },
    "result" : {
        "data": [
                <list of objects> # here must be listed the single model
            ]
    }
}

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

一种可能的方法是使用 fields.Raw which returns 您传递的任何可序列化对象。然后,您可以定义第二个函数,它创建您的结果并使用 marshalmarshal 根据模型转换您的数据并接受名为 envelope 的附加参数。 envelope 通过给定的键围绕您的建模数据并完成技巧。

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)

解决我所有问题的解决方法:

我创建了一个新的列表字段class(它主要是从fields.List复制的),然后我只是调整输出格式和架构以获得'data'作为关键:

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