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.
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With