Creating a Form Class
A form class defines the fields users fill in when adding or editing a record. It maps to your model and is used by BaseCrudView for both the Add form and Edit row actions.
Imports
from ...packages.crud.forms import BaseForm
from ...packages.crud.form_fields import ModelField
from .models import Patient
:::info Relative Import Depth
| Module location | Dots needed |
|---|---|
backend/patients/forms.py | from ...packages (3 dots) |
backend/masters/geography/forms.py | from ....packages (4 dots) |
| ::: |
Basic Form Class
class PatientForm(BaseForm):
name = ModelField(
placeholder="Enter Name",
required=True,
required_msg="Name is required.",
)
contact_person = ModelField(
placeholder="Enter Contact Person",
required=True,
required_msg="Contact person is required.",
)
address = ModelField(
placeholder="Enter Address",
required=False,
)
class Meta:
model = Patient
Meta.model links the form to your model. ModelField automatically maps to the correct field type based on the model definition.
Custom Fields
Use CustomSchemaField when you need full control over a field's schema — for rich text areas, custom widgets, or fields not on the model:
from ...packages.crud.form_fields import ModelField, CustomSchemaField
class PatientForm(BaseForm):
bio = CustomSchemaField(
required=False,
schema={"type": "string", "title": "Bio"},
ui_schema={"ui:widget": "TextareaFieldWidget"},
)
class Meta:
model = Patient
Django Form Fields
You can also mix in standard Django form fields:
import django.forms as forms
class PatientForm(BaseForm):
notes = forms.CharField(label="Notes", required=False)
class Meta:
model = Patient