Creating models — Camelot documentation

### Navigation

- index
- modules |
- next |
- previous |
- Camelot documentation »
- Camelot Documentation contents »
- Camelot Documentation »

# Creating models

Camelot makes it easy to create views for any type of Python objects. An important part of Camelot is introspection on Python objects mapped to a database with SQLAlchemy. This allows a developer to only define his SQLAlchemy model and the views will reflect all properties defined in the model, such as field types and relations.

SQLAlchemy is a very powerful Object Relational Mapper (ORM) with lots of possibilities for handling complex and large datastructures. The SQLAlchemy website has extensive documentation on all these features.

To facilitate the use of SQLAlchemy in less advanced use cases, like the Active Record Pattern, layers exist on top SQLAlchemy to make those things easy and the complex things still possible. One such layer is Elixir. Most of the code samples in the documentation make use of Elixir, an Elixir model definition is very simple:

    class GeographicBoundary( Entity ):
        """The base class for Country and City"""
        using_options( tablename = 'geographic_boundary' )
        code = Field( Unicode( 10 ) )
        name = Field( Unicode( 40 ), required = True )

        @ColumnProperty
        def full_name( self ):
            return self.code + ' ' + self.name

        def __unicode__( self ):
            return u'%s %s' % ( self.code, self.name )

The code above defines the model for a GeographicBoundary class, a base class that will be used later on to subclass into Countries and Cities, and is part of the default Persons and Organizations data model of Camelot. This code has some things to notice :

> - GeographicBoundary is a subclass of Entity, Entity is the base class for all classes that are mapped to the database
> - the using_options statement allows us to fine tune the ORM, in this case the GeographicBoundary class will be mapped to the geographic_boundary table
> - The Field statement add fields of a certain type, in this case Unicode, to the GeographicBoundary class as well as to the geographic_boundary table
> - The ColumnProperty full_name is a more advanced feature of the ORM, when an GeographicBoundary object is read from the database, the ORM will ask the database to concatenate the fields code and name, and map the result to the full_name attribute of the object
> - 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

The Elixir website provides a complete overview of the creation of models with elixir.

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

- Fields
  - SQLAlchemy field types
  - Camelot field types
- Calculated Fields
  - Python properties as fields
  - Attach actions to field changes
  - Fields calculated by the database
- Views
  - The model to start from
  - Definition of the view
  - Put into action
- Under the hood
  - Setting up the model
    - Metadata
    - Engine
    - Setup model
  - Working without the default model
  - Using Camelot without the GUI

### Navigation

- index
- modules |
- next |
- previous |
- Camelot documentation »
- Camelot Documentation contents »
- Camelot Documentation »

Comments
blog comments powered by Disqus