Skip to content
Advertisement

Flask-Restx not converting enum field type to JSON

I need help with Enum field type as it is not accepted by Swagger and I am getting error message **TypeError: Object or Type eGameLevel is not JSON serializable**. Below is the complete set of code for table. Complete set of code with DB table and sqlalchemy settings is provided. I already tried it with Marshmallow-Enum Flask package and it didn’t worked. Looking for kind help with some explanation about the solution so I can learn it well. :-)

I am using MySQL with the Flask. In Postgres its pretty easy to manage all the choice fields. All I need is a working example or a link to repository where MySQL choice fields are showing up in swagger drop down.

My Model:

JavaScript

My Schema

JavaScript

My Resources:

JavaScript

Advertisement

Answer

Instead of trying to manage Enum fields for MySQL schema I suggest to use another table with backref to your eGameLevel. You can get rid of this whole fuss and also in future if you needed to add another option in your choice field you won’t have to hardcode it.

Simply create a main table as Game and sub table as eGameLevel (with only one string field). You will be able to access choices from your Game table.

Whenever I get stuck I go to basics as mentioned in here.

Advertisement