-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Django integration | ||
================== | ||
|
||
.. code-block:: python | ||
import fsm | ||
from django.db import models | ||
class MyModel(models.Model, fsm.FiniteStateMachineMixin): | ||
"""An example to test the state machine. | ||
Contains transitions to everywhere, nowhere and specific states. | ||
""" | ||
CHOICES = ( | ||
('created', 'CREATED'), | ||
('pending', 'PENDING'), | ||
('running', 'RUNNING'), | ||
('success', 'SUCCESS'), | ||
('failed', 'FAILED'), | ||
('retry', 'RETRY'), | ||
) | ||
state_machine = { | ||
'created': '__all__', | ||
'pending': ('running',), | ||
'running': ('success', 'failed'), | ||
'success': None, | ||
'failed': ('retry',), | ||
'retry': ('pending', 'retry'), | ||
} | ||
state = models.CharField(max_length=30, choices=CHOICES, default='created') | ||
def on_change_state(self, previous_state, next_state, **kwargs): | ||
self.save() | ||
Django Rest Framework | ||
--------------------- | ||
|
||
If you are using :code:`serializers`, they usually perform the :code:`save`, so saving inside :code:`on_change_state` is not necessary. | ||
|
||
One simple solution is to do this: | ||
|
||
.. code-block:: python | ||
class MySerializer(serializers.ModelSerializer): | ||
def update(self, instance, validated_data): | ||
instance = super().update(instance, validated_data) | ||
new_state = validated_data.get('state', instance.state) | ||
try: | ||
instance.change_state(new_state) | ||
except fsm.InvalidTransition: | ||
raise serializers.ValidationError("Invalid transition") | ||
return instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ Contents | |
readme | ||
installation | ||
usage | ||
django | ||
reference/index | ||
contributing | ||
authors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters