form.extensions

Attributes

_FormT

form_extensions

Classes

FormExtension

Enables the extension of form definitions/submissions.

Extendable

Models extending their form classes use this mixin to create the

Module Contents

form.extensions._FormT[source]
form.extensions.form_extensions: dict[str, type[FormExtension[Any]]][source]
class form.extensions.FormExtension(form_class: type[_FormT])[source]

Bases: Generic[_FormT]

Enables the extension of form definitions/submissions.

When either of those models create a form class they will take the ‘extensions’ key in the meta dictionary to extend those formcode based forms.

This allows for specialised behaviour of formcode forms with the drawback that those definitions/submissions are more tightly bound to the code. That is to say code in module A could not use submissions defined by module B unless module B is also present in the path.

To create and register a form extension subclass as follows:

class MyExtension(FormExtension, name='my-extension'):
    def create(self):
        return self.form_class

Note that you should not change the form_class provided to you. Instead you should subclass it. If you need to change the form class, you need to clone it:

class MyExtension(FormExtension, name='my-extension'):
    def create(self):
        return self.form_class.clone()

class MyExtension(FormExtension, name='my-extension'):
    def create(self):
        class ExtendedForm(self.form_class):
            pass

        return ExtendedForm

Also, names must be unique and can only be registered once.

classmethod __init_subclass__(name: str, **kwargs: object)[source]
abstract create() type[_FormT][source]
class form.extensions.Extendable[source]

Models extending their form classes use this mixin to create the extended forms. It also serves as a marker to possibly keep track of all classes that use extended forms.

extend_form_class(form_class: type[_FormT], extensions: Collection[str]) type[_FormT][source]