Search code examples
pythonflaskflask-sqlalchemyflask-admin

How to generate a model from PostgreSQL JSON data type for using it with Flask-Admin


I'm using a PostgreSQL database with a column defined as JSON that acts as a datastore for a Flask app, using SQLAlechemy. My idea was creating a model for this column, so I can interact with it pretty much like you do with MongoEngine and MongoDB. My goal is to provide that model to Flask-Admin.

With MongoEngine you can do something like:

class User(Document):
    email = StringField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)

I want to do something similar with the JSON column in PostreSQL. Is that possible?


Solution

  • In any case, Flask-Admin wants schema for your models - it needs to know which columns are available.

    I'd suggest implementing custom model backend and use pymongo backend as an example: https://github.com/mrjoes/flask-admin/tree/master/flask_admin/contrib/pymongo

    It is not that hard to do (under 150 lines of code).