Not every CRM form needs to be a multi-step wizard. In many cases, users simply need to open a form, enter a few details, save it, and continue working.
This is where Horilla CRM‘s HorillaSingleFormView comes in. It provides a simple way to build create and edit forms while taking care of many common requirements such as permissions, HTMX modal handling, dynamic forms, file uploads, and related-object creation.
Instead of writing the same form-handling code for every model, you can build a reusable create/edit view with just a few configurations.
What is HorillaSingleFormView?
HorillaSingleFormView is a class-based view designed for single-step create and edit forms in Horilla.
It is built on top of FormViewCommonMixin and Horilla’s FormView, so several common operations are already handled by the framework.
For example, a typical form can automatically:
- Create or edit a model instance
- Render inside an HTMX modal
- Handle hx-post, hx-target, and hx-swap
- Check create and edit permissions
- Support “own record” permissions
- Add related objects directly from FK and M2M fields
- Display dynamic condition rows
- Preserve uploaded files when validation fails
- Support a “Save & New” option
- Redirect to a detail page after saving
- Connect to a multi-step form when the workflow becomes more complex
This means the view itself can stay quite small.
A Simple Example
Let’s look at a basic create/edit view for a lead stage:
class CreateLeadStage(LoginRequiredMixin, HorillaSingleFormView):
model = LeadStatus
form_class = LeadStatusForm
modal_height = False
def get_initial(self):
initial = super().get_initial()
if not self.kwargs.get("pk"):
company = self.request.user.company
if company:
initial["order"] = LeadStatus.get_next_order_for_company(company)
return initial
@cached_property
def form_url(self):
pk = self.kwargs.get("pk") or self.request.GET.get("id")
if pk:
return reverse_lazy(
"leads:edit_lead_stage",
kwargs={"pk": pk},
)
return reverse_lazy("leads:create_lead_stage")
There is no need to manually implement post(), add permission checks, or write the HTMX response handling.
The view mainly defines what is being edited and any custom behavior required for that particular form.
Configuring the Form
HorillaSingleFormView provides several attributes that can be used to control how the form behaves.
Some of the commonly used ones are:
| Attribute | Purpose |
| model | Specifies the Django model used by the form |
| form_class | Specifies an existing form class |
| fields / exclude | Controls which model fields appear when using a dynamic form |
| full_width_fields | Displays selected fields across the full form width |
| dynamic_create_fields | Allows users to create related FK/M2M records directly |
| form_title | Overrides the default create/edit title |
| hidden_fields | Keeps selected fields out of the rendered form |
| permission_required | Allows a custom permission to be specified |
| check_object_permission | Controls object-level permission checking |
| skip_permission_check | Skips the default permission check when required |
| multi_step_url_name | Connects the form to a multi-step form |
| detail_url_name | Redirects to the created object’s detail page |
| save_and_new | Enables the Save & New option |
| modal_height / modal_height_class | Controls the size of the modal |
You only need to configure the options that are relevant to your particular form.
You Don’t Always Need a Form Class
One useful feature is that HorillaSingleFormView does not always require a separate Django ModelForm.
If form_class isn’t provided, the view can create a form dynamically using single_form_builder.get_dynamic_form_class.
For example, a simple view can define the model and fields:
class CreateForecastTarget(HorillaSingleFormView):
model = ForecastTarget
fields = ["employee", "target", "year"]
Horilla can build the form based on the model and the configured fields.
This is particularly useful for straightforward forms where you don’t need much custom validation or special form behavior.
When the form needs custom validation, widgets, or more complex processing, you can still provide a normal form_class.
Permissions Are Already Handled
Permission checking is another area where HorillaSingleFormView saves a lot of repetitive code.
The shared FormViewCommonMixin takes care of the standard create and edit permission checks.
For a new record, Horilla checks permissions such as:
add_<model>
For an existing record, it checks:
change_<model>
The framework also supports the corresponding “own record” permissions where ownership applies.
Ownership can be determined using configured OWNER_FIELDS or an is_owned_by method on the model.
Superusers bypass these checks.
If the user doesn’t have permission, Horilla can return the 403.html response as a modal fragment instead of showing an unrelated full-page error.
In most cases, you therefore don’t need to write permission logic directly inside every form view.
Adding Related Records from the Form
Sometimes a form contains a ForeignKey or ManyToMany field, but the related record doesn’t exist yet.
For example, while creating a contact, the user may realize that the required company hasn’t been added to the CRM.
dynamic_create_fields can be used for these situations.
It allows supported FK/M2M fields to provide an inline option for creating the related record, so the user doesn’t have to leave the current form and manually navigate somewhere else.
This makes the overall create workflow much smoother.
Building Forms with Conditions
Some CRM features need more than the normal model fields.
Scoring rules, workflow conditions, and similar features may require users to add multiple conditions such as:
Field → Operator → Value
HorillaSingleFormView has support for this type of condition-based form.
By configuring options such as:
condition_fields
condition_model
condition_related_name
condition_field_choices
the view can handle the additional condition rows along with the main form.
After the main object is saved, the related condition records can also be persisted.
This avoids having to build a completely separate form-handling system for these rule-builder scenarios.
Extending Forms Without Modifying the Original View
Another useful part of Horilla’s form architecture is its extension support.
When a normal form_class is used, get_form_class() goes through Horilla’s form resolution mechanism.
This allows an extension app to provide an inherited version of an existing form.
For example:
_inherit_form = “app.forms.TicketForm”
An extension can then customize the form without directly modifying the original application code.
This is especially useful when building custom modules or extensions that need to add fields or change existing form behavior while keeping the core implementation untouched.
Dynamic model-only forms are different because there is no static form class to extend.
What Happens When the User Saves?
The save process follows the normal Django form flow, but HorillaSingleFormView adds Horilla-specific handling around it.
The form is first validated and the object is saved using the usual commit=False pattern.
Depending on the model, Horilla can also handle values such as:
- created_by
- updated_by
- company
After the main object is saved, ManyToMany values and any configured condition records can also be processed.
If everything succeeds, the response depends on the configuration.
By default, the modal can be closed and the underlying list refreshed.
If detail_url_name is configured, Horilla can instead redirect to the newly created object’s detail page.
For forms using save_and_new, the form can be reset so that the user can immediately create another record.
The view also handles certain IntegrityError cases and can turn unique-constraint failures into form-level errors instead of allowing them to become an unhandled server error.
When a Single Form Isn’t Enough
A single form works well for most straightforward create and edit operations.
But sometimes the amount of information required is too large to comfortably fit into one form.
That’s where HorillaMultiStepFormView becomes useful.
A single-step view can be connected to its multi-step counterpart using:
multi_step_url_name
Both views share FormViewCommonMixin, so important behavior such as permission checking, dynamic related-object creation, and condition handling can remain consistent.
The main difference is simply how the information is presented to the user.
HorillaSingleFormView is useful when you want to build a clean create or edit experience without repeating the same form-handling code across different views.
For simple forms, you can let Horilla generate the form dynamically. When more customization is needed, you can provide your own form class.
The view also takes care of common CRM requirements such as permissions, HTMX modal responses, related-object creation, condition rows, file handling, and different save flows.
So, whenever a feature needs a straightforward create or edit form, HorillaSingleFormView is a good starting point. If the workflow later becomes more involved, it can also be connected to a multi-step form without having to rebuild the underlying form infrastructure.