Serial Marshmallow Analyzer

I'm trying to exclude columns in the Flask-Restless API using a native deserializer with Marshmallow as suggested by the docs :

serializers.py

class HeatSchema(Schema):
    id = fields.Integer()
    heat_index = fields.Integer()
    updated_at = fields.DateTime()

class Meta:
    exclude = ('updated_at',)

def make_object(self, data):
    print 'Making object from', data
    return Heat(**data)

      

server.py

from serializers import HeatSchema

heat_schema = HeatSchema()

def heat_serializer(instance):
    return heat_schema.dump(instance).data

def heat_deserializer(data):
    return heat_schema.load(data).data

apimanager = APIManager(app, flask_sqlalchemy_db=db)

apimanager.create_api(
    heat,
    methods=['GET'],
    url_prefix='/api/v1',
    collection_name='heat',
    results_per_page=10,
    serializer=heat_serializer,
    deserializer=heat_deserializer
)

      

I get the same response from the API, no matter what I do with the Heat schema. I can put

blahblah = fields.Integer()

      

without any changes. I can't even hit a breakpoint in the serializer while debugging, so I'm guessing I set this setting incorrectly with Flask-Restless?

+3


source to share


2 answers


I also had the same problem. It seems that the behavior only appears for the GET_MANY functions. If you try to get one instance of an object, it must match the marshmallow schema. This is the weird behavior that was reported here on the flash alert bug tracker: https://github.com/jfinkels/flask-restless/issues/167

In this case, user itsrifat suggested a workaround for adding a post processor:


person_schema = PersonSchema()
      



def person_deserializer(data):

  return person_schema.load(data).data







def person_after_get_many(result=None, search_params=None, **kw):

result['objects'] = [person_deserializer(obj) for obj in result['objects']]





apimanager.create_api(Person,methods=['GET', 'POST','PUT', 'DELETE'],

  postprocessors={

   'GET_MANY':[person_after_get_many]

  }

)





code>
+3


source


The Meta class must be inside the Schema class.

Meta: where config is added to the schema class.



 class HeatSchema(Schema):
     id = fields.Integer()
     heat_index = fields.Integer()
     updated_at = fields.DateTime()

     class Meta:
         exclude = ('updated_at',)

     def make_object(self, data):
         print 'Making object from', data
         return Heat(**data)

      

0


source







All Articles