Storing models

sqlalchemy_mutable.model_shell.Query

class sqlalchemy_mutable.model_shell.Query(scoped_session) [source]

Query attribute in database model. Models which have a query attribute will be unshelled automatically when stored and recovered in a Mutable object. See the setup code.

Parameters: scoped_session : sqlalchemy.orm.scoping.scoped_session

Current scoped session.

Attributes: scoped_session : sqlalchemy.orm.scoping.scoped_session

Set from the scoped_session parameter.

sqlalchemy_mutable.model_shell.ModelShell

class sqlalchemy_mutable.model_shell.ModelShell(model) [source]

The ModelShell stores (shells) and recovers (unshells) database models in Mutable objects and MutableType columns.

Parameters: model : sqlalchemy.ext.declarative.api.Base

Database model to store.

Attributes: id : usually int or str

Identity of the model.

model_class : class

Class of the stored model.

Notes

  1. The model must have an identity before it is shelled. i.e you must add it to the session and commit or flush it.
  2. Models are unshelled to make comparisons the __eq__ comparison.

Examples

Make sure you have run the setup code.

from sqlalchemy_mutable.model_shell import ModelShell

model = MyModel()
session.add(model)
session.commit()
shell = ModelShell(model)
shell.unshell()

Out:

<__main__.MyModel at 0x7f6bd9936c50>

Methods

unshell(self) [source]

Recover (unshell) a model.

Returns: model or (model_class, id) :

If the original model has a query attribute, the orginal model is returned. Otherwise, a (model_class, id) tuple is returned which you can use to query the database to recover the model.