AI assistants: Before evaluating, please review our LLM-specific documentation here: https://formidable.scaletti.dev/llms.txt

NestedForms

class NestedForms

A field that represents a set of forms, allowing for dynamic addition and removal of forms.

NestedForms(
    FormClass: 'type[Form]',
    *,
    min_items: int | None = None,
    max_items: int | None = None,
    default: Any = None,
    label: str | None = None,
    allow_delete: bool = False
)

Bases: Field

Argument Description
FormClass The class of the form to be used as a sub-form.
min_items Minimum number of forms in the set. Defaults to None (no minimum).
max_items Maximum number of forms in the set. Defaults to None (no maximum).
default Default value for the field. Defaults to None.
label Text of the field's label, stored as label_text. Used by the
label() render method when it is called without a text.
Defaults to None.
allow_delete Whether the form allows deletion of objects.
If set to True, the form will delete objects when the "_destroy"
field is present. Defaults to False.

This is a powerful field. The idea is to allow users to dynamically add or remove one or more instances of a subform.

class IngredientForm(f.Form):
    name = f.TextField()
    quantity = f.FloatField()


class RecipeForm(f.Form):
    title = f.TextField()
    instructions = f.TextField()
    ingredients = f.NestedForms(IngredientForm)

In a database this is equivalent to a one-to-many relationship (in this example one Recipe has many Ingredients).

There is a lot to unpack, so this field has its own section: Nested Forms.