Behaviors

Behaviors are reusable pieces of functionality that can be used via a model and can react to model events. Behaviors are an analogue of components designed for models instead of controllers.

Using Behaviors

To use a behavior, include it in your model’s meta class:

class Post(Model):
    class Meta:
        behaviors = (SomeBehavior,)

Most behavaiors also get their configuration from the meta class:

class Meta:
    behaviors = (SomeBehavior,)
    somebehavior_someconfig = 'test'

Creating Behaviors

To create a behavior subclass Behavior. Here’s a example that underscores the specified fields in Meta.underscore_fields:

from ferris import Behavior

class Underscorer(Behavior):
    def before_put(self, instance):
        for field in instance.Meta.underscore_fields:
            val = getattr(instance, field)
            underscored = inflector.underscore(val)
            setattr(instance, field, underscored)
class ferris.core.ndb.behavior.Behavior(Model)[source]

Behavior mimics all of the callbacks in the Model class to allow you to stack unrelated callbacks together

Behaviors can respond to the same callbacks as models.

Behavior.before_put(self, instance)[source]
Behavior.after_put(self, instance)[source]
Behavior.before_delete(self, key)[source]
Behavior.after_delete(self, key)[source]
Behavior.before_get(self, key)[source]
Behavior.after_get(self, item)[source]