Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 7eedf07e96
Fetching contributors…

Cannot retrieve contributors at this time

66 lines (43 sloc) 2.67 KB

Creating models

Camelot makes it easy to create views for any type of Python objects.

SQLAlchemy is a very powerful Object Relational Mapper (ORM) with lots of possibilities for handling simple or sophisticated datastructures. The SQLAlchemy website has extensive documentation on all these features. An important part of Camelot is providing an easy way to create views for objects mapped through SQLAlchemy.

SQLAlchemy comes with the Declarative extension to make it easy to define an ORM mapping using the Active Record Pattern. This is used through the documentation and in the example code. An alternative to Declarative is Elixir, which was used in previous Camelot versions, and is still supported.

To use Declarative, threre are some base classes that should be imported:

Those are :

Next a model can be defined:

The code above defines the model for a Tag class, an object with only a name that can be related to other ojbects later on. This code has some things to notice :

  • Tag is a subclass of :class:`camelot.core.orm.Entity`,
  • the __tablename__ class attribute allows us to specify the name of the table in the database in which the tags will be stored.
  • The Column statement add fields of a certain type, in this case Unicode, to the Tag class as well as to the tags table
  • The __unicode__ method is implemented, this method will be called within Camelot whenever a textual representation of the object is needed, eg in a window title or a many to one widget. It's good practice to always implement the __unicode__ method for all Entity subclasses.

When a new Camelot project is created, the :ref:`camelot-admin` tool creates an empty file that can be used as a place to start the model definition.

Jump to Line
Something went wrong with that request. Please try again.