Package: django-fsm-logger
Django Finite State Machine LogAutomatic logging for the excellentDjango FSMpackage.Logs can be accessed before a transition occurs and before they are persisted to the database
by enabling a cached backend. SeeAdvanced UsageChangelog2.0.2-dev (unreleased)2.0.1 (2020-03-26)Add support for django3.0Drop support for python21.6.2 (2019-01-06)Address Migration history breakage added in 1.6.11.6.1 (2018-12-02)Make StateLog.description field nullable1.6.0 (2018-11-14)Add source state on transitionsFixedget_state_displaywith FSMIntegerField (#63)Fixed handling of transitions if target is None (#71)Addedfsm_log_descriptiondecorator (#1, #67)Dropped support for Django 1.10 (#64)1.5.0 (2017-11-29)cleanup deprecated code.add codecov support.switch to pytest.add Admin integration to visualize past transitions.1.4.0 (2017-11-09)Bring compatibility with Django 2.0 and drop support of unsupported versions
of Django:1.6,1.7,1.9.CompatibilityPython 2.7 and 3.4+Django 1.8+Django-FSM 2+InstallationFirst, install the package with pip. This will automatically install any
dependencies you may be missingpipinstalldjango-fsm-logRegister django_fsm_log in your list of Django applications:INSTALLED_APPS=(...,'django_fsm_log',...,)Then migrate the app to create the database tablepythonmanage.pymigratedjango_fsm_logUsageThe app listens for thedjango_fsm.signals.post_transitionsignal and
creates a new record for each transition.To query the log:fromdjango_fsm_log.modelsimportStateLogStateLog.objects.all()# ...all recorded logs...Disabling logging for specific modelsBy default transitions get recorded for all models. Logging can be disabled for
specific models by adding their fully qualified name toDJANGO_FSM_LOG_IGNORED_MODELS.DJANGO_FSM_LOG_IGNORED_MODELS=('poll.models.Vote',)for_Manager MethodFor convenience there is a customfor_manager method to easily filter on the generic foreign key:frommy_app.modelsimportArticlefromdjango_fsm_log.modelsimportStateLogarticle=Article.objects.all()[0]StateLog.objects.for_(article)# ...logs for article...byDecoratorWe found that our transitions are commonly called by a user, so we've added a
decorator to make logging this easy:fromdjango.dbimportmodelsfromdjango_fsmimportFSMField,transitionfromdjango_fsm_log.decoratorsimportfsm_log_byclassArticle(models.Model):state=FSMField(default='draft',protected=True)@fsm_log_by@transition(field=state,source='draft',target='submitted')defsubmit(self,by=None):passWith this the transition gets logged when thebykwarg is present.article=Article.objects.create()article.submit(by=some_user)# StateLog.by will be some_userAdmin integrationThere is an InlineForm available that can be used to display the history of changes.To use it expand your ownAdminModelby addingStateLogInlineto its inlines:fromdjango.contribimportadminfromdjango_fsm_log.adminimportStateLogInline@admin.register(FSMModel)classFSMModelAdmin(admin.ModelAdmin):inlines=[StateLogInline]Advanced UsageYou can change the behaviour of this app by turning on caching for StateLog records.
Simply addDJANGO_FSM_LOG_STORAGE_METHOD = 'django_fsm_log.backends.CachedBackend'to your project's settings file.
It will use your project's default cache backend by default. If you wish to use a specific cache backend, you can add to
your project's settings:DJANGO_FSM_LOG_CACHE_BACKEND='some_other_cache_backend'The StateLog object is now available after thedjango_fsm.signals.pre_transitionsignal is fired, but is deleted from the cache and persisted to the database afterdjango_fsm.signals.post_transitionis fired.This is useful if:you need immediate access to StateLog details, and cannot wait untildjango_fsm.signals.post_transitionhas been firedat any stage, you need to verify whether or not the StateLog has been written to the databaseAccess to the pending StateLog record is available via thepending_objectsmanagerfromdjango_fsm_log.modelsimportStateLogarticle=Article.objects.get(...)pending_state_log=StateLog.pending_objects.get_for_object(article)Running Testspipinstalltox
tox