Automatic logging for the excellent Django FSM package.
First, install the package with pip. This will automatically install any dependencies you may be missing
pip install django-fsm-log
With Python 2.7 Django 1.4+ is supported, with Python 3.3 Django 1.5+ is supported.
Then migrate the app to create the database table
python manage.py migrate django_fsm_log
This app will listen for the django_fsm.signals.post_transition
to be fired
and create a new record for each transition. To query logs simply
from django_fsm_log.models import StateLog
StateLog.objects.all()
# ...all recorded logs...
For convenience there is a custom for_
manager method to easily filter on the generic foreign key
from my_app.models import Article
from django_fsm_log.models import StateLog
article = Article.objects.all()[0]
StateLog.objects.for_(article)
# ...logs for article...
We found that our transitions are commonly called by a user, so we've added a decorator to make logging that painless
from django.db import models
from django_fsm.db.fields import FSMField, transition
from django_fsm_log.decorators import fsm_log_by
class Article(models.Model):
state = FSMField(default='draft', protected=True)
@fsm_log_by
@transition(field=state, source='draft', target='submitted')
def submit(self, by=None):
pass
Then every time the transition is called with the by
kwarg set, it will be logged
article = Article.objects.create()
article.submit(by=some_user) # StateLog.by will be some_user
./runtests.py