Skip to content

Form Components

Purpose: Show one Django form as a Glue component, so the same form can be opened in a modal, placed on a page, or handed to a list to create and edit its rows.


Why Form Components?

A form shown in a modal needs its fields, its values, its validation and its save to reach the browser together. The form components provide:

  • A component around a Form or a ModelForm, with the form as its Glue child
  • Create and edit from one component: no primary key creates, a primary key edits
  • A component built straight from a form class and a template, with no subclass
  • Hooks for choosing the row, the exposed fields and the choices of relation fields
  • A path to the form that templates read, so a form partial works wherever it is included
  • Options that tell a scroll component to use a form component or a form page

Quick Start

1. Give the form a save the browser can call

from django.forms import ModelForm
from django_glue import Glue, GlueResponse
from django_glue.message import GlueMessage

from app.task.models import Task


class TaskForm(ModelForm):
    saved = Glue.event()

    @Glue.attr(required_access=Glue.Access.required_save_access)
    def save_model_obj(self) -> GlueResponse:
        if not self.is_valid():
            return GlueResponse(messages=[GlueMessage.error('Invalid Fields')])

        task, _created = self.instance.services.save_model_obj(**self.cleaned_data)
        self.saved(pk=task.pk)

        return GlueResponse(result={'pk': task.pk})

    class Meta:
        model = Task
        fields = ['name', 'status']

2. Write the form's templates

The fields, in task/form/task_form.html:

{% extends 'django_spire/glue/form/modal_form.html' %}

{% block glue_form_on_saved %}
    if (result?.pk) {
        Spire.notify.success(`Task ${this.glue_form.$pk ? 'updated' : 'added'} successfully!`)
    }
{% endblock %}

{% block glue_form_content %}
    <div class="row g-2">
        <div class="col-12">
            {% include 'django_spire/glue/form/field/char/field.html' with glue_field='glue_form.name' %}
        </div>
        <div class="col-12">
            {% include 'django_spire/glue/form/field/choice/field.html' with glue_field='glue_form.status' %}
        </div>
    </div>
{% endblock %}

The modal content around them, in task/modal/content/task_form_modal_content.html:

{% extends 'django_spire/modal/content/title_modal_content.html' %}

{% block modal_title %}
    {% if component.model_pk %}Edit Task{% else %}New Task{% endif %}
{% endblock %}

{% block modal_body %}
    {% include 'task/form/task_form.html' %}
{% endblock %}

The component's own template, in task/component/task_form_modal.html:

<div>
    {% include 'task/modal/content/task_form_modal_content.html' %}
</div>

3. Give the form to a list

from django_spire.core.components import ModelCrudScrollComponent


class TaskListComponent(ModelCrudScrollComponent):
    item_form_options = ModelCrudScrollComponent.FormOptions(
        form_class=TaskForm,
        template='task/component/task_form_modal.html',
    )

The list's New and Edit buttons now open the form in a modal, and a saved row appears or updates in the list.


Core Concepts

ModelFormComponent

A component around a ModelForm. The row is its model child, a Glue model, and the form hangs off that at component.model.form.

from django_spire.core.components import ModelFormComponent
Member Description
form_class The ModelForm class. The model is the form's own
template The component's template
model_pk A parameter. None creates a row, a value edits that row. pk is its deprecated name
fields The fields the Glue model exposes. Defaults to the form's fields
get_model() Return the row. A blank one, or the row with model_pk, by default
get_fields() Return the exposed fields
get_required_access() ADD while model_pk is None, CHANGE otherwise. Read by django-glue's ModelAccessRequiredMixin

A row that no longer exists is reported as Glue's model_instance_not_found, a 404.

To let in only a user who may do what the form does, mix in django-glue's ModelAccessRequiredMixin. It sets the access from the user's permissions on the form's model, and turns away a user without add_* for a new row or change_* for an existing one, on the page load and on every call, so the same component guards itself on a page and in a list's modal:

class TaskFormComponent(Glue.ModelAccessRequiredMixin, ModelFormComponent):
    template = 'task/component/task_form_modal.html'
    form_class = TaskForm

FormComponent

A component around a plain Form, for a form that is not tied to a row. The form is its form child, at component.form.

from django_spire.core.components import FormComponent
Member Description
form_class The Form class
template The component's template
get_form() Return the form. Override it when the form needs constructor arguments

BaseFormComponent

The parent of both. It resolves the form class and the template, however they were given, and declares glue_form_path().

from django_spire.core.components import BaseFormComponent

glue_form_path() returns the path from the template to the form: component.model.form or component.form. A form partial reads it as component.glue_form_path, and django_spire/glue/form/modal_form.html uses it by default, so the same partial works under either component.

glue_form_object_name is the name the form gets in the template's Alpine scope, glue_form by default. Set it on the component to name the form after what it edits:

class AccountFormComponent(ModelFormComponent):
    form_class = AccountForm
    glue_form_object_name = 'account_form'

The partial then reads its fields as account_form.name, and form.html and modal_form.html validate, save and report through this.account_form. A glue_form_object_name passed in the template context still wins.

FormOptions and PageFormOptions

Tell a CRUD scroll how its rows are created and edited. Both are class attributes of ModelCrudScrollComponent, so a list needs no other import.

Options Arguments Description
ModelCrudScrollComponent.FormOptions component The application's own form component class, in a modal
ModelCrudScrollComponent.FormOptions form_class, template A form class and a template, shown in a modal by ModelFormComponent itself
ModelCrudScrollComponent.PageFormOptions url_name, create_url_name, return_url_name A form page. url_name takes the row's key as pk

FormOptions takes a component, or a form class together with a template, and refuses any other combination when it is created.


Main Operations

Building a Form Component Without a Subclass

form = ModelFormComponent(
    form_class=TaskForm,
    template='task/component/task_form_modal.html',
    model_pk=task.pk,
)

The form class is carried to the browser as its import path, so it must be defined at the top level of a module. A form class defined inside a function is refused.

Writing a Form Component Class

class TaskFormComponent(ModelFormComponent):
    template = 'task/component/task_form_modal.html'
    form_class = TaskForm

Write a class when the component needs hooks of its own, or parameters beyond model_pk.

Opening a Form From a Callable

from django_glue import Glue


class TaskBoardComponent(Glue.Component):
    template = 'task/component/task_board.html'

    @Glue.attr(required_access=Glue.Access.CHANGE)
    def task_form(self, pk: int | None = None) -> TaskFormComponent:
        return TaskFormComponent(model_pk=pk, access=self.access)
<button
    class="btn btn-sm btn-outline-secondary"
    type="button"
    @click="
        const form = await component.task_form({pk: {{ task.pk }}})

        form.model.form.$on('saved', () => Spire.modal.close())
        await Spire.modal.dispatchGlueComponent(form)
    "
>
    Edit
</button>

The callable returns the form component and the page shows it. The form component closes nothing itself: the page listens for the form's saved event and closes the modal.

Scoping or Pre-filling the Row

class TaskFormComponent(ModelFormComponent):
    template = 'task/component/task_form_modal.html'
    form_class = TaskForm

    def get_model(self) -> Task:
        if self.model_pk is None:
            return Task(assigned_to=self.request.user)

        return Task.objects.active().get(pk=self.model_pk, assigned_to=self.request.user)

get_model() picks the row. Override it to give a new row its defaults, or to limit which rows the component will open. The default looks a row up by primary key with no other condition.

Exposing Fewer Fields

class TaskFormComponent(ModelFormComponent):
    template = 'task/component/task_form_modal.html'
    form_class = TaskForm
    fields = ('name',)

fields narrows what the Glue model exposes to the browser. Override get_fields() when it depends on the request.

Narrowing a Relation Field's Choices

class TaskForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['project'].queryset = Glue.choices(
            Project.objects.active().filter(team=self.instance.team),
            search_fields=['name'],
        )

The form says which rows a relation field may choose from, as any Django form does: narrow the field's queryset in __init__. The component builds the form with the row as self.instance, so the choices can depend on it. The form's dropdown offers that queryset, and the form refuses a value outside it when it validates. Wrap it in Glue.choices(..., search_fields=[...]) to make a large table searchable.


Upgrading From 1.3

The form components were reworked in 1.4.0. A subclass written for 1.3 updates these names.

In 1.3 In 1.4
from django_spire.core.glue.components import ... from django_spire.core.components import ...
form = TaskForm form_class = TaskForm
attr = 'time_entry' Removed. The child is always model on a ModelFormComponent and form on a FormComponent
component.time_entry.form in a template component.model.form, or better, component.glue_form_path
model_class = Task Removed. The model is the form's own
fields = (...), required Optional. Defaults to the form's fields
build_child() Removed. Override get_form(), or the hooks on ModelFormComponent
ModelFormComponent inherits from FormComponent Both inherit from BaseFormComponent

A template that reads the form through component.glue_form_path, or that extends modal_form.html, needs no change.