Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does django-fsm call save() method after state changed?

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?

like image 439
Ivan Hreskiv Avatar asked Oct 25 '25 08:10

Ivan Hreskiv


2 Answers

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('/')
like image 60
Satendra Avatar answered Oct 26 '25 21:10

Satendra


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.

like image 39
David D. Avatar answered Oct 26 '25 21:10

David D.