Creating a Workflow
A workflow class defines the full lifecycle of a model — its statuses, which transitions are allowed, and what logic runs during them. Create it in a workflow.py file inside your module.
Import
from ...packages.workflow.base.engine import WorkflowBase
:::info Relative Import Depth
| Module location | Dots needed |
|---|---|
backend/patients/workflow.py | from ...packages (3 dots) |
backend/masters/orders/workflow.py | from ....packages (4 dots) |
| ::: |
Minimal Example
from ...packages.workflow.base.engine import WorkflowBase
from .models import Patient
class PatientWorkflow(WorkflowBase):
status_transitions = [
{
"name": "inactivate",
"display_name": "Deactivate",
"description": "Deactivate the patient record",
"confirmation_message": "Are you sure you want to deactivate?",
"from": "active",
"to": "inactive",
},
{
"name": "activate",
"display_name": "Activate",
"description": "Activate the patient record",
"confirmation_message": "Are you sure you want to activate?",
"from": "inactive",
"to": "active",
},
]
class Meta:
model = Patient
on_create_status = "active" # status assigned when a record is created
statuses = {
"active": {"color": "#12B76A", "label": "Active"},
"inactive": {"color": "#F04438", "label": "Inactive"},
}
Attach to a CRUD View
Import and attach the workflow class to your BaseCrudView:
from ...packages.crud.base import BaseCrudView
from .tables import PatientTable
from .forms import PatientForm
from .workflow import PatientWorkflow
class PatientCrudView(BaseCrudView):
page_title = "Patients"
add_btn_title = "Add Patient"
table = PatientTable
form = PatientForm
workflow = PatientWorkflow # ← attach here
Once attached, the CRUD view automatically:
- Sets
on_create_statuswhen a new record is created - Shows available transitions as action buttons in the detail view
- Renders the current status badge via
WorkflowStatuson the frontend
Full Structure Summary
| Part | Where | Purpose |
|---|---|---|
status_transitions | list on the class | All allowed moves between statuses |
Meta.model | inner class | The model this workflow manages |
Meta.on_create_status | inner class | Status assigned on record creation (required) |
Meta.statuses | inner class | Status display labels and colours |
<name>_condition() | method | Gate — can this transition happen? |
<name>_done() | method | Hook — runs after transition completes |
tag_transitions | list on the class | Optional tag enable/disable rules |
Meta.tags | inner class | Optional secondary flags |