Model helper registry

Provides a Registry type and a RegistryMixin base class with three registries, used by other mixin classes.

Helper classes such as forms and views can be registered to the model and later accessed from an instance:

class MyModel(BaseMixin, db.Model):
    ...

class MyForm(Form):
    ...

class MyView(ModelView):
    ...

MyModel.forms.main = MyForm
MyModel.views.main = MyView

When accessed from an instance, the registered form or view will receive the instance as an obj parameter:

doc = MyModel()
doc.forms.main() == MyForm(obj=doc)
doc.views.main() == MyView(obj=doc)

The name main is a recommended default, but an app that has separate forms for new and edit actions could use those names instead.

class coaster.sqlalchemy.registry.Registry(param: Optional[str] = None, property: bool = False, cached_property: bool = False)[source]

Container for items registered to a model.

clear_cache_for(obj) → bool[source]

Clear cached instance registry from an object.

Returns True if cache was cleared, False if it wasn’t needed.

class coaster.sqlalchemy.registry.InstanceRegistry(registry, obj)[source]

Container for accessing registered items from an instance of the model.

Used internally by Registry. Returns a partial that will pass in an obj parameter when called.

clear_cache()[source]

Clear cache from this registry.

class coaster.sqlalchemy.registry.RegistryMixin[source]

Adds common registries to a model.

Included:

  • forms registry, for WTForms forms
  • views registry for view classes and helper functions
  • features registry for feature availability test functions.

The forms registry passes the instance to the registered form as an obj keyword parameter. The other registries pass it as the first positional parameter.