user1700840
user1700840

Reputation: 451

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?

Upvotes: 0

Views: 1169

Answers (2)

Joes
Joes

Reputation: 1818

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).

Upvotes: 1

Vor
Vor

Reputation: 35109

It is possible, take a look at http://json-schema.org/ What you can do is to validate each JSON against predefined schema. And then insert it into DB.

Upvotes: 0

Related Questions