Use dynamic source and target states
mainThe source and target parameters support advanced logic:
- Wildcards:
source='*': Any state (constantfsm.ANY_STATE).source='+': Any state except the target (constantfsm.ANY_OTHER_STATE).
- Dynamic Targets:
fsm.RETURN_VALUE(value_if_returned, target_state): Uses the method's return value to determine the target.fsm.GET_STATE(callable, states=[...]): Uses a callable to determine the target state from a list of allowed states.
import django_fsm as fsm
# Using RETURN_VALUE
@fsm.transition(
field=state,
source='*',
target=fsm.RETURN_VALUE('for_moderators', 'published'),
)
def publish(self, is_public=False, **kwargs):
return 'for_moderators' if is_public else 'published'
# Using GET_STATE
@fsm.transition(
field=state,
source='for_moderators',
target=fsm.GET_STATE(
lambda self, allowed: 'published' if allowed else 'rejected',
states=['published', 'rejected'],
),
)
def moderate(self, allowed, **kwargs):
pass