【问题标题】:Using Flask-Restful's fields, how do I have a "catchall" field?使用 Flask-Restful 的字段,我如何拥有一个“catchall”字段?
【发布时间】:2016-04-26 22:02:34
【问题描述】:

我正在使用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 模式的“无模式”部分?

【问题讨论】:

    标签: python json rest flask flask-restful


    【解决方案1】:

    啊哈。你可以使用fields.Raw

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      相关资源
      最近更新 更多