Fixtures : handling static data in the database

Some tables need to be filled with default data when users start to work with the application. The Camelot fixture module camelot.model.fixture assist in handling this kind of data.

Suppose we have an entity PartyCategory to divide Persons and Organizations into certain groups.

The complete definition of such an entity can be found in camelot.model.authentication.PartyCategory.

To make things easier for the first time user, some prefab categories should be available when the user starts the application. Such as Suspect, Prospect, VIP.

When to update fixtures

Most of the time static data should be created or updated right after the model has been set up and before the user starts using the application.

The easiest place to do this is in the setup_model method inside the settings.py module.

So we rewrite settings.py to include a call to a new update_fixtures method:

def update_fixtures():
  """Update static data in the database"""
  from camelot.model.fixture import Fixture
  from model import MovieType

def setup_model():
  from camelot.model import *
  from camelot.model.memento import *
  from camelot.model.synchronization import *
  from camelot.model.authentication import *
  from camelot.model.i18n import *
  from camelot.model.fixture import *
  from model import *
  setup_all(create_tables=True)
  updateLastLogin()
  update_fixtures()

Creating new data

When creating new data with the fixture module, a reference to the created data will be stored in the fixture table along with a ‘fixture key’. This fixture key can be used later to retrieve or update the created data.

So lets create some new movie types:

def update_fixtures():
  """Update static data in the database"""
  from camelot.model.fixture import Fixture
  from model import MovieType
  Fixture.insertOrUpdateFixture(MovieType,
                                fixture_key = 'comic',
                                values = dict(name='Comic'))
  Fixture.insertOrUpdateFixture(MovieType,
                                fixture_key = 'scifi',
                                values = dict(name='Science Fiction'))

Fixture keys should be unique for each Entity class.

Update fixtures

When a new version of the application gets released, we might want to change the static data and add some icons to the movie types. Thanks to the ‘fixture key’, it’s easy to retrieve and update the already inserted data, just modify the update_fixtures function:

def update_fixtures():
  """Update static data in the database"""
  from camelot.model.fixture import Fixture
  from model import MovieType
  Fixture.insertOrUpdateFixture(MovieType,
                                fixture_key = 'comic',
                                values = dict(name='Comic', icon='spiderman.png'))
  Fixture.insertOrUpdateFixture(MovieType,
                                fixture_key = 'scifi',
                                values = dict(name='Science Fiction', icon='light_saber.png'))

The fixture version

In case lots of data needs to be read into the database (like a list of postal codeds), it might make no sense to create a new fixture for each code, instead a fixture version number can be set to indicate a list has been read into the database. The camelot.model.fixture.FixtureVersion exists to facilitate this.

        import csv
        if FixtureVersion.get_current_version( u'demo_data' ) == 0:
            reader = csv.reader( open( example_file ) )
            for line in reader:
                Person( first_name = line[0], last_name = line[1] )
            FixtureVersion.set_current_version( u'demo_data', 1 )
            session.flush()

Table Of Contents

Previous topic

Built in data models

Next topic

Managing a Camelot project

This Page