Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask RestPlus inherit model doesn't work as expected

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.

like image 389
4c74356b41 Avatar asked Dec 08 '16 18:12

4c74356b41


People also ask

What is flask-restplus?

What is Flask-RESTPlus? Flask-RESTPlus is an extension for Flask that adds support for quickly building REST APIs. Flask-RESTPlus encourages best practices with minimal setup. It provides a coherent collection of decorators and tools to describe your API and expose its documentation properly (using Swagger).

How do I use flask-restplus with blueprint?

Flask-RESTPlus provides a way to use almost the same pattern as Blueprint. The main idea is to split your app into reusable namespaces. A namespace module will contain models and resources declaration. line 6 creates a new user dto through the model interface provided by the api namespace in line 5.

What is flask-script?

Flask-Script: An extension that provides support for writing external scripts in Flask and other command-line tasks that belong outside the web application itself. What is Flask-RESTPlus? Flask-RESTPlus is an extension for Flask that adds support for quickly building REST APIs. Flask-RESTPlus encourages best practices with minimal setup.

What is the difference between Flask-bcrypt and flask-migrate?

Flask-Bcrypt: A Flask extension that provides bcrypt hashing utilities for your application. Flask-Migrate: An extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are made available through the Flask command-line interface or through the Flask-Script extension.


1 Answers

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.

like image 140
4c74356b41 Avatar answered Oct 23 '22 10:10

4c74356b41