Created
November 11, 2017 18:18
-
-
Save treyd/f24eed880b43e9debe81b6651690612f to your computer and use it in GitHub Desktop.
Example of using django-fsm and transitions on a django model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from django_fsm import FSMField, transition | |
from django.db import models | |
logger = logging.getLogger(__name__) | |
class MyStatefulModel(models.Model): | |
class STATE: | |
INIT = 'Init' | |
RUN = 'Running' | |
STOP = 'Stopped' | |
STATE_CHOICES = (STATE.INIT, STATE.RUN, STATE.STOP) | |
state = FSMField(default=STATE.INIT, choices=STATE_CHOICES) | |
@transition(field=state, source=STATE.INIT, target=STATE.RUN) | |
def run(self): | |
logger.info('Running!') | |
@transition(field=state, source=STATE.RUN, target=STATE.STOP) | |
def stop(self): | |
logger.info('Stopping!') | |
@transition(field=state, source='*', target=STATE.INIT) | |
def reset(self): | |
logger.info('Resetting to INIT') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I use it with django-rest-framework in the views