Flask RestPlus inherit model doesn't work as expected

4.4k views Asked by At

So I have this model in Flask RestPlus:

NS = Namespace('parent')
PARENT_MODEL = NS.model('parent', {
    'parent-id': fields.String(readOnly=True,
    'parent-name': fields.String(required=True)
})
CHILD_MODEL = NS.inherit('child', SUBSCRIPTION_MODEL, {
    'child-id': fields.String(required=True, readOnly=True),
    'child-name': fields.String(required=True),
    'child-some-property': fields.String(required=True)
})

CHILD_PROPERTY_MODEL = NS.inherit('child-other-property', RESOURCE_GROUP_MODEL, {
    'child-other-property': fields.Raw(required=False)
})

It doesn't work as expected, I get this output (and similar structure on the swagger docs).

[
  {
    "parent-id": "string",
    "parent-name": "string",
    "child-id": "string",
    "child-name": "string",
    "child-some-property": "string",
    "child-other-property": {}
  }
]

instead of something like this:

[
  {
    "parent-id": "string",
    "parent-name": "string", {
        "child-id": "string",
        "child-name": "string",
        "child-some-property": "string",{
            "child-other-property": {}
      }
    }
  }
]

I'm probably missing something simple, but can't understand what. This is what I'm consulting to figure out Models in Flask Restplus.

2

There are 2 answers

0
4c74356b41 On BEST ANSWER
NS = Namespace('sample')

child_model = NS.model('child', {
    'childid': fields.String(required=True, readOnly=True),
    'childname': fields.String(required=True),
    'data': fields.String(required=True),
    'complexdata': fields.Raw(required=False)
})

parent_model = NS.model('parent', {
    'id': fields.String(readOnly=True),
    'name': fields.String(required=True),
    'childdata': fields.List(
        fields.Nested(child_model, required=True)
        )
})

this is what works for me. It appears that Flask Restplus github is dead, no answer from maintainers. This might help someone.

0
Preethi Lakku On

This how I declared the nested fields in a serializer.py file

from flask_restplus import fields

from api.restplus import api

child2 = api.model('child2', {
    'child2name': fields.Url(description='child2 name'),

})
child1= api.model('child1', {
    'child2': fields.Nested(child2)
})

parent = {
    'name': fields.String(description='name'),
     'location': fields.String(description='location details'),
}
parent ["child1"] = fields.Nested(child1)

resource_resp = api.model('Response details', parent )

Usage in view.py, I am marshaling/generating the json with @api.marshal_with(resource_resp)

from flask import request, jsonify
from flask_restplus import Resource
from serializers import *

ns = api.namespace('apiName', description='API Description')

@ns.route('/route/<some_id>')
class ResourceClient(Resource):    
    @ns.response(401, "Unauthorized")
    @ns.response(500, "Internal Server Error")
    @api.doc(params={'some_id': 'An ID'})
    @api.marshal_with(resource_resp )
    def get(self, some_id):
        """
        Do GET
        """
        # Logic
        return {"status" : "success"}