A lot of things happen under the hood when a model is defined using Elixir, and picked up by Camelot :
Each file that contains a part of the model definition should contain these lines :
They associate the Entities defined in this file with the default metadata. The metadata is a datastructure that contains information about the database in which the tables for the model will be created.
The settings.py file should contain a function named ENGINE that returns a connection to the database. This connection will be associated with the default metadata used in the model definition.
As such, all defined models are associated with this database.
When the application starts up, the setup_model function in the settings.py file is called. In this function, all model files should be imported, to make sure the model has been completely setup.
Camelot comes with a default model for Persons, Organizations, History tracking, etc.
You might want to turn this off, here’s how to do so :
import elixir
from camelot.core.orm import Session
elixir.session = Session
Transactions in Camelot can be used just as in normal SQLAlchemy. This means that inside a camelot.admin.action.Action.model_run() method a transaction can be started and committed
model_context.session.begin()
...do some modifications...
model_context.session.commit()
More information on the transactional behavior of the session can be found in the SQLAlchemy documentation
Camelot contains a method decorator (camelot.core.sql.transaction() to decorate methods on the model definition to be executed within a transaction
class Person( Entity ):
@transaction
def merge_with( self, other_person ):
...
Often a Camelot application also has a non GUI part, like batch scripts, server side scripts, etc.
It is of course perfectly possible to reuse the whole model definition in those non GUI parts. The easiest way to do so is to leave the Camelot GUI application as it is and then in the non GUI script, initialize the model first
import settings settings.setup_model()
From that point, all model manipulations can be done. Access to the session can be obtained via any Entity subclass, such as Person
session = Person.query.session
After the manipulations to the model have been done, they can be flushed to the db
session.flush()