I write API on Flask-RESTplus using Flask-Marshmallow . There is a need to add some information to the response generated through schema.dump(some_data) . For example, I have a Flask-SQLAlchemy model:

 class User(mixins.PaginatedAPIMixin, db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) username = db.Column(db.String(45), nullable=False, unique=True) password_hash = db.Column(db.String(128), unique=True) 

There is also a resource:

 @api.route('/', endpoint='users') class UsersResource(Resource): @api.param('page', description='Page number :int:') @api.param('per_page', description='Items per page :int:') def get(self): args = parse_get_args(page=int, per_page=int) users = User.get_paginated(User.query, self, args['page'], args['per_page']) schema = UsersSchema(many=True) dumped_data = schema.dump(users['items']).data dumped_data.append({'_links': users['_links'], '_meta': users['_meta']}) return schema.jsonify(dumped_data) 

UsersSchema is ModelSchema from Flask-Marshmallow (pages are for example, as pagination goes to mixins.PaginatedAPIMixin ):

 class UsersSchema(ma.ModelSchema): class Meta: model = User fields = ('id', 'username') 

so, in the get_paginated() method of the get_paginated() resource, in the users variable, as a result of calling the get_paginated() method of the model, this dictionary is stored ( items are user objects):

 {'items': items, '_meta': { 'page': 1, 'per_page': 20, 'total_pages': 1, 'total_items': 2 }, '_links': { 'self': 'api/v1/users/?page=1&per_page=20', 'next': None, 'prev': None }} 

the snag is that at least dumped_data.append({'_links': users['_links'], '_meta': users['_meta']}) and adds the dictionary to the list, but a subsequent call to schema.jsonify(dumped_data) cuts in it all fields not specified in ModelSchema ... As a result, it turns out this:

 [ { "id": 1, "username": "spam" }, { "id": 2, "username": "egg" }, {} ] 

And I would like this:

 [ "items": [ { "id": 1, "username": "spam" }, { "id": 2, "username": "egg" } ], "_links": { "self": "api/v1/users/?page=1&per_page=20" }, "_meta": { "page": 1, "per_page": 20, "total_pages": 1, "total_items": 2 }, ] 

Through self.declared_fields.update(additional_fields) in the constructor, ModelSchema tried, displays only if the added new field is already contained in the fields tuple ... While the only solution is to use not schema.jsonify() , but flask.jsonify() , then everything is given OK, but it seems to me that this is somehow crooked.

    0