SQLAlchemy & Elixir

Note: This assumes that you have Elixir installed and enabled in your web framework. Refer to your framework’s documentation for details on how to do this.

Define Model

Add the following to your model file. The actual file depends on the framework:

import elixir as el

class Movie(el.Entity):
    title = el.Field(el.String(100))
    description = el.Field(el.Text)
    year = el.Field(el.Integer)
    genera = el.Field(el.String(100))
    release_date = el.Field(el.Date)

el.setup_all()

Elixir automatically creates an integer primary key column, called id. Note: el.setup_all() may not be necessary, depending on your framework.

When you’ve done this, you need to tell your web framework to create the database tables, see the framework-specific instructions.

Load Data

In the movie controller method, replace the following lines:

# TBD: load movie from database
movie = None

With:

movie = id is not None and db.Movie.get(id)
if id is not None and not movie:
    raise Exception('Movie ID not found')

Save Data

In the save_movie controller method, replace the line:

# TBD: save movie to database

With:

if id is not None:
    movie = db.Movie.get(id)
    if not movie:
        raise Exception('Movie ID not found')
else:
    movie = db.Movie()
movie.from_dict(kw)