CRUD with Flask-Diamond

Create Read Update Delete

CRUD stands for Create, Read, Update, and Delete. The CRUD pattern complements Model-View-Controller by providing a standard set of methods that can be applied to most types of models. CRUD acts like a simple API for models in Flask-Diamond. The four CRUD actions describe the life-cycle of a model object:

  1. First, an object is created with certain attribute values.
  2. The object may be read to get the value of those object attributes.
  3. The object values may be updated to update the model based on changes in the world.
  4. Finally, the object is deleted to remove it from the database.

CRUDMixin

flask_diamond.mixins.crud.CRUDMixin, which has been adapted from Flask-Kit, will extend your model with functions for create(), read(), update(), and delete(). The default Flask-Diamond scaffold provides an example of CRUDMixin usage:

from flask.ext.diamond.utils.mixins import CRUDMixin
from .. import db

class Person(db.Model, CRUDMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    age = db.Column(db.Integer)

Notice that class Person() inherits both db.Model and CRUDMixin. Now the following CRUD workflow is possible:

myself = Person.create(name="me", age=34) # create
print "{name} is {age} years old".format(myself)
myself.update({'age': 35}) # update
print "{name} is now {age} years old".format(myself)
myself.delete()

Flask-Admin CRUD

Flask-Admin provides Model CRUD functionality with its BaseModelView class. BaseModelView is an extremely powerful tool for rapidly implementing a web-based CRUD Graphical User Interface that makes it easy to create, read, update, and delete model objects using a web browser.

The following application instantiates a CRUD for the Person model described above.

from flask.ext.diamond import Diamond, db
from flask.ext.diamond.administration import AdminModelView
from . import models

class my_diamond_app(Diamond):
    def administration(self):
        admin = super(my_diamond_app, self).administration()
        admin.add_view(AdminModelView(
            models.Person,
            db.session,
            name="Person",
            category="Admin"
            )
        )

Further Reading

Table Of Contents

Topic Navigation

Version