使用Flask-Restful的字段,我如何拥有“catchall”字段?

时间:2016-04-26 22:02:34

标签: python json rest flask flask-restful

我正在使用Flask-Restful。我想要一个结构化的元组,但是有一个任意字段可以包含任何其他有效的JSON数据。

例如,假设我为具有'name'和'birth_date'的用户提供了资源,但也包含一个包含任意JSON的'preferences'字段:

from flask_restful import Resource, marshal_with, fields
import datetime
class MyResource(Resource):
    resource_fields = {
        'name': fields.String,
        'birth_date': fields.DateTime,
        'preferences': fields.GeneralJson
    }

    @marshal_with(resource_fields)
    def get(self):
        return {'name': 'Newbie Newborn',
                'birth_date': datetime.datetime.now(),
                'preferences': {
                    'wine': 'cab-sav',
                    'cheese': 'cheddar',
                    'favorite_movies': ['The Big Lebowski', 'Anchorman']}
                }

当然,问题在于fields.GeneralJson不存在。如何使用Flask-Restful来定义JSON模式的“无模式”部分?

1 个答案:

答案 0 :(得分:1)

啊哈。您可以使用fields.Raw