django_fsm.viewset.FlowViewsMixin exposes transitions as actions in
Viewflow Pro frontend views. This optional integration requires Viewflow Pro
and its templates and static files.
The examples use Separate Flow Classes, including the on_success callback that
saves the new state.
from django_fsm.viewset import FlowViewsMixin
from viewflow.urls import ModelViewset
from .flow import ReportFlow
from .models import Report
class ReportViewset(FlowViewsMixin, ModelViewset):
model = Report
flow_state = ReportFlow.state_field
list_display = ['id', 'text', 'state_field']
The mixin constructs ReportFlow(obj) for each object and adds available
transitions to the detail page. Transition views check permissions and
conditions. Override get_object_flow(self, request, obj) if the flow needs
additional context.
Transition forms have no editable fields by default. Allow fields explicitly:
def get_transition_fields(self, request, obj, slug):
return ['text'] if slug == 'approve' else []
For a custom form, subclass django_fsm.viewset.ModelTransitionView, override
get_form_class() or set form_class, and assign that class to the
viewset’s transition_view_class attribute. Saving the form and running the
transition take place in one database transaction.
The transition view looks for <app_label>/<model_name>_transition.html,
then falls back to viewflow/views/transition.html. Set template_name
on a custom transition view to select another template.
The mixin adds a chart/ URL and a “State chart” action on the list page.