Search code examples
pythonflaskflask-restful

Django serialisers but for any python application


I've moved from working on a Django application onto a Flask app using 'connexion'.

I'm missing Django rest framework serialisers greatly. Is there something else that I can use without installing Django?

thanks


Solution

  • Use Marshmallow!

    Django's doc says:

    The django-rest-marshmallow package provides an alternative implementation for serializers, using the python marshmallow library. It exposes the same API as the REST framework serializers, and can be used as a drop-in replacement in some use-cases.

    Since you tagged your question flask-restful, I should add that both flask-restful and its fork flask-restplus are great frameworks to build a REST API with serialization/deserialization and even documentation generation among other features. They have their own marshaling and argument parsing methods. It has been suggested here and there that they move to Marshmallow and Marshmallow-based webargs for these tasks but as of today, this is still on the TODO list.

    Altenatively, you may chose to rely on the Marshmallow ecosystem.

    • Marshmallow for serialization / deserialization
    • webargs (Marshmallow inside) for argument parsing
    • apispec for API documentation

    You'll still lack a nice integration layer. flask-apispec aims at being this integration layer. Unfortunately, it is not mature enough, but there seems to be a bit of activity on it recently.

    Edit: flask-smorest is an extra layer above marshmallow/webargs/apispec that brings the marshmallow stack to the same level as flask-resful/restplus., though more recent and not as stable/mature.

    Disclaimer: marshmallow/webargs/apispec/flask-smorest maintainer.