Skip to content

Instantly share code, notes, and snippets.

@treyd
Created November 11, 2017 18:18
Show Gist options
  • Save treyd/f24eed880b43e9debe81b6651690612f to your computer and use it in GitHub Desktop.
Save treyd/f24eed880b43e9debe81b6651690612f to your computer and use it in GitHub Desktop.
Example of using django-fsm and transitions on a django model
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')
@pmutua
Copy link

pmutua commented Apr 19, 2018

How do I use it with django-rest-framework in the views

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment