FormActionMixin¶
FormActionMixin
¶
djadmin.actions.base.FormActionMixin
Mixin for actions that display an embedded custom form (Pattern A).
This is for actions that show a simple form (typically in a modal/dialog) where the action itself handles form processing. The form is usually a plain django.forms.Form (not ModelForm) and submission is handled by the action's execute() method.
Use this for: ChangeStatusAction, SendEmailAction, BulkUpdateAction NOT for: AddAction, EditRecordAction (use ViewActionMixin instead)
Class Attributes
| Attribute | Type | Description |
|---|---|---|
form_class |
- | Form class to use (required for this mixin) |
template_name |
- | Template to use (optional, uses default if None) |
Method Resolution Order
djadmin.actions.base.FormActionMixin
Attributes
| Attribute | Value | Defined in |
|---|---|---|
__annotations__ |
{'form_class': type[django.forms.forms.Form] | None, 'tem... |
djadmin.actions.base.FormActionMixin
|
form_class |
None |
djadmin.actions.base.FormActionMixin
|
template_name |
None |
djadmin.actions.base.FormActionMixin
|
Methods
form_invalid(self, request: django.http.request.HttpRequest, form: django.forms.forms.Form, **kwargs) -> django.http.response.HttpResponse
Defined in:
<class 'djadmin.actions.base.FormActionMixin'>
Handle invalid form submission.
Default behavior: re-render form with errors.
Args: request: The HTTP request form: The invalid form instance **kwargs: Additional context
Returns: HttpResponse with form errors
Source code
in base.py
line 402
def form_invalid(self, request: HttpRequest, form: Form, **kwargs) -> HttpResponse:
"""
Handle invalid form submission.
Default behavior: re-render form with errors.
Args:
request: The HTTP request
form: The invalid form instance
**kwargs: Additional context
Returns:
HttpResponse with form errors
"""
from django.shortcuts import render
context = {
'form': form,
'action': self,
'opts': self.model._meta,
**kwargs,
}
return render(request, self.get_template_name(), context)
form_valid(self, request: django.http.request.HttpRequest, form: django.forms.forms.Form, **kwargs) -> django.http.response.HttpResponse
Defined in:
<class 'djadmin.actions.base.FormActionMixin'>
Handle valid form submission.
Override this method to implement the action logic.
Args: request: The HTTP request form: The valid form instance **kwargs: Additional context (obj, queryset, etc.)
Returns: HttpResponse (typically a redirect)
Source code
in base.py
line 386
def form_valid(self, request: HttpRequest, form: Form, **kwargs) -> HttpResponse:
"""
Handle valid form submission.
Override this method to implement the action logic.
Args:
request: The HTTP request
form: The valid form instance
**kwargs: Additional context (obj, queryset, etc.)
Returns:
HttpResponse (typically a redirect)
"""
raise NotImplementedError(f'{self.__class__.__name__} must implement form_valid()')
get_form(self, request: django.http.request.HttpRequest, **kwargs) -> django.forms.forms.Form
Defined in:
<class 'djadmin.actions.base.FormActionMixin'>
Instantiate form for display or processing.
Args: request: The HTTP request **kwargs: Additional form kwargs
Returns: Form instance
Source code
in base.py
line 358
def get_form(self, request: HttpRequest, **kwargs) -> Form:
"""
Instantiate form for display or processing.
Args:
request: The HTTP request
**kwargs: Additional form kwargs
Returns:
Form instance
"""
form_class = self.get_form_class()
if request.method == 'POST':
return form_class(request.POST, request.FILES, **kwargs)
return form_class(**kwargs)
get_form_class(self) -> type[django.forms.forms.Form]
Defined in:
<class 'djadmin.actions.base.FormActionMixin'>
Get form class for this action.
Returns: Form class
Source code
in base.py
line 347
def get_form_class(self) -> type[Form]:
"""
Get form class for this action.
Returns:
Form class
"""
if self.form_class is None:
raise ValueError(f'{self.__class__.__name__} must define form_class when using FormActionMixin')
return self.form_class
get_template_name(self) -> str
Defined in:
<class 'djadmin.actions.base.FormActionMixin'>
Get template name for form view.
Returns: Template path string
Source code
in base.py
line 375
def get_template_name(self) -> str:
"""
Get template name for form view.
Returns:
Template path string
"""
if self.template_name:
return self.template_name
return 'djadmin/actions/form_modal.html'
Fields
| Field | Type | Related To |
|---|---|---|
__dict__ |
getset_descriptor |
- |
__weakref__ |
getset_descriptor |
- |