Models

Models are responsible for persisting data and containing application logic.

Conventions

Models are named with singular nouns (for example: Page, User, Image, Bear, etc.). Breaking convention is okay, but scaffolding will not work properly without some help.

Each model class should be in it’s own file under /app/models and the name of the file should be the underscored class name. For example, to create a model to represent furry bears, you would create the file /app/models/furry_bear.py and inside of that define a class named FurryBear.

Ferris’ Model Class

The Model class is built on top of App Engine’s google.appengine.ext.ndb module and there is nothing stopping you from directly using any regular ndb.Model class with Ferris. Documentation on propeties, querying, etc. can be found here.

class ferris.core.ndb.Model(*args, **kwds)

Base class that augments ndb Models by adding easier find methods and callbacks.

classmethod Model.find_all_by_properties(**kwargs)

Generates an ndb.Query with filters generated from the keyword arguments.

Example:

User.find_all_by_properties(first_name='Jon',role='Admin')

is the same as:

User.query().filter(User.first_name == 'Jon', User.role == 'Admin')
classmethod Model.find_by_properties(**kwargs)

Similar to find_all_by_properties, but returns either None or a single ndb.Model instance.

Example:

User.find_by_properties(first_name='Jon',role='Admin')

Automatic Methods

The Model class automatically generates a find_by_[property] and a find_all_by_[property] classmethod for each property in your model.These are shortcuts to the above methods.

For example:

Show.find_all_by_title("The End of Time")
Show.find_all_by_author("Russell T Davies")

Callbacks

Additionally the Model class provides aliases for the callback methods. You can override these methods in your Model and they will automatically be called after their respective action.

Model.before_put()

Called before an item is saved.

Parameters:self – refers to the item that is about to be saved
Note :self.key is invalid if the current item has never been saved
Model.after_put(key)

Called after an item has been saved.

Parameters:
  • self – refers to the item that has been saved
  • key – refers to the key that the item was saved as
classmethod Model.before_get(key)

Called after an item is retrieved.

Parameters:key – Is the key of the item that is to be retrieved.
classmethod Model.after_get(key, item)

Called after an item has been retrieved.

Parameters:
  • key – Is the key of the item that was retrieved.
  • item – Is the item itself.
classmethod Model.before_delete(key)

Called before an item is deleted.

Parameters:key – is the key of the item that is about to be deleted. It is okay to get() this key to interogate the properties of the item.
classmethod Model.after_delete(key)

Called after an item is deleted.

Parameters:key – is the key of the item that was deleted. It is not possible to call get() on this key.

These methods are useful for replicating database triggers, enforcing application logic, validation, search indexing, and more.

Access Fields

The BasicModel adds automatic access fields:

class ferris.core.ndb.BasicModel(*args, **kwds)

Adds the common properties created, created_by, modified, and modified_by to Model

ferris.core.ndb.BasicModel.created

Stores the created time of an item as a datetime (UTC)

ferris.core.ndb.BasicModel.modified

Stores the modified time of an item as a datetime (UTC)

ferris.core.ndb.BasicModel.created_by

Stores the user (a google.appengine.api.users.User) who created an item.

ferris.core.ndb.BasicModel.modified_by

Stores the user (a google.appengine.api.users.User) who modified an item.

Table Of Contents

Previous topic

MVC in Ferris

Next topic

Handlers

This Page