4

I am using django_fsm to manage state in my model. My model looks like:

from django.db import models,
from django_fsm import FSMField, transition


class MyModel(models.Model):
    STATES = (
        ('pending', _('Pending')),
        ('active', _('Active'))
    )  
    state = FSMField(choices=STATES, default='pending', protected=True) 

    @transition(field=state, source='pending', target='active')
    def change_state(self):
        pass

Should I add self.save() to change_state? Will it be called?

Ivan Hreskiv
  • 1,045
  • 1
  • 11
  • 18

2 Answers2

5

If calling change_state() succeeds without raising an exception, the state field will be changed, but not written to the database.

So for making changes into database you need to call obj.save() explicitly

def change_view(request, model_id):
    obj = get_object__or_404(MyModel, pk=model_id)
    obj.change_state()
    obj.save()
    return redirect('/')
Satendra
  • 6,755
  • 4
  • 26
  • 46
2

You can use a post_transition signal to handle this:

from django_fsm.signals import post_transition

@receiver(post_transition, sender=models.MyModel)
def save_new_workflow_state(sender, instance, name, source, target, **kwargs):
    """ Save the new workflow state following successful django_fsm transition. """

    if source != target:
        instance.save()

This comes from this issue.

David Dahan
  • 10,576
  • 11
  • 64
  • 137