Finite state machines define a set of states and transitions between them.
viewflow.fsm enforces these rules at runtime—methods only run when the
current state allows it.
FSM works well for simple sequential workflows. For parallel execution or complex branching, use BPMN workflows instead.
The State class holds a value from a Python enum or Django choices class.
You can’t change it with direct assignment—only through transitions.
from enum import Enum
from viewflow.fsm import State
class States(Enum):
NEW = 1
DONE = 2
HIDDEN = 3
class MyFlow(object):
state_field = State(States, default=States.NEW)
@state_field.transition(source=States.NEW, target=States.DONE)
def complete():
pass
@state_field.transition(source=State.ANY, target=States.HIDDEN)
def hide():
pass
flow = MyFlow()
flow.state_field == States.NEW # True
flow.state_field = States.DONE # Raises AttributeError
flow.complete()
flow.state_field == States.DONE # True
flow.complete() # Raises TransitionNotAllowed
The @transition decorator adds runtime checks. Methods can only run when
the object is in the right state.