Ken Kinder
Ken Kinder

Reputation: 13140

Using Flask-Restful's fields, how do I have a "catchall" field?

I'm using Flask-Restful. I'd like to have a structured marshal, but with an arbitrary field that could contain any other valid JSON data.

For example, suppose that I have a resource for users with 'name' and 'birth_date', but also a 'preferences' field that contains any arbitrary JSON:

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']}
                }

The problem with this, of course, is that fields.GeneralJson does not exist. What should I do to define a "schema-less" section of a JSON schema using Flask-Restful?

Upvotes: 2

Views: 255

Answers (1)

Ken Kinder
Ken Kinder

Reputation: 13140

Aha. You can just use fields.Raw.

Upvotes: 2

Related Questions