Skip to main content

Extension to manage SQLAlchemy models in Flask

Project description

Flask-Model-Management

  • This is a Flask extension for managing Flask-SQLAlchemy models.
  • This extension allows the user to perform CRUD (create, read, update, delete) operations on the models registered using the UI

The frontend is built using:

NB: all the above JS & CSS are bundled into this package currently

The backend is built using:

  • Flask-SQLAlchemy & SQLAlchemy
  • Flask-WTF & WTForms

Status: In Alpha Development

BE WARNED: INSTALLING CRUD APPLICATIONS INTO PRODUCTION SERVERS ALLOWS USERS TO PERFORM POTENTIALLY IRREVERSIBLE DATA OPERATIONS

Install

pip install:

pip install flask_model_management

Use

  • an example of how to use without app factory might look something like:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_model_management import ModelManager

app = Flask(__name__)
# from flask_sqlalchemy docs
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
model_manager = ModelManager(app, db)

# showing model declaration for the example
class User(db.Model):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True)
    username = Column(String)

model_manager.register_model(User)
  • with app factory:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_model_management import ModelManager

db = SQLAlchemy()
model_manager = ModelManager()

# showing model declaration for the example
class User(db.Model):
    __tablename__ = "user"

    id = Column(Integer, primary_key=True)
    username = Column(String, nullable=False)

def create_app():
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
    db.init_app(app)
    model_manager.init_app(app, db)

    model_manager.register_model(User, nullable=False)

UI Examples

  • Home page home page

  • Table page table page

  • Create (single entry) page create single page

  • Read (bulk entries) page read bulk page

  • Update (single) page update single page

  • Update (bulk) page update single page

  • Delete (single) page delete single page

  • Delete (bulk) page delete single page

How it works

frontend perspective

  • Once you plug-in this extension then at: <YOUR-BASE-URL>/model-management/ you will find the home dashboard
  • You can browse around the pages and perform CRUD operations on your models

backend perspective

  • A blueprint is added to your app called model_management with the url prefix /model-management
  • 4 url routes are added to this blueprint:
    • A home page at: /
    • A table page at: /<tablename>
    • An operation page at: /<tablename>/<operation>
      • Operations limited to: create, read, update or delete
    • An operation API at: /api/<tablename>
      • CRUD operations are mapped to HTTP methods:
        • POST = create
        • GET = read
        • PUT = update
        • DELETE = delete
  • The data from the frontend forms are then sent via ajax request to the operation API with the required data and HTTP method
  • WARNING: this library will therefore wrap your Flask-SQLAlchemy models with an API endpoint
  • There are 2 'protocols': single & bulk
    • single is for CRUD applied to one entry
      • always applied to: CREATE
      • sometimes applied to: UPDATE, DELETE
    • bulk is for CRUD applied to multiple entries (and is default in all but CREATE)
      • always applied to: READ
      • sometimes (default) applied to: UPDATE, DELETE

Todo

  • re-add decorators to models
  • excluded columns
  • excluded operations

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Model-Management-0.1.0.tar.gz (158.5 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page