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.
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'
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)
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.