88import django_fsm as fsm
99from django_fsm import GET_STATE
1010from django_fsm import RETURN_VALUE
11- from django_fsm import FSMField
12- from django_fsm import FSMKeyField
1311from django_fsm import transition
1412
1513
@@ -19,7 +17,7 @@ class Application(models.Model):
1917 Test workflow
2018 """
2119
22- state = FSMField (default = "new" )
20+ state = fsm . FSMField (default = "new" )
2321
2422 @transition (field = state , source = "new" , target = "published" , on_error = "failed" )
2523 def standard (self ) -> None :
@@ -106,7 +104,7 @@ class FKApplication(models.Model):
106104 Test workflow for FSMKeyField
107105 """
108106
109- state = FSMKeyField (DbState , default = "new" , on_delete = models .CASCADE )
107+ state = fsm . FSMKeyField (DbState , default = "new" , on_delete = models .CASCADE )
110108
111109 @transition (field = state , source = "new" , target = "published" )
112110 def standard (self ) -> None :
@@ -175,7 +173,7 @@ def on_error(self) -> None:
175173
176174
177175class MultiStateApplication (Application ):
178- another_state = FSMKeyField (DbState , default = "new" , on_delete = models .CASCADE )
176+ another_state = fsm . FSMKeyField (DbState , default = "new" , on_delete = models .CASCADE )
179177
180178 @transition (field = another_state , source = "new" , target = "published" )
181179 def another_state_standard (self ) -> None :
@@ -198,7 +196,7 @@ class BlogPost(models.Model):
198196 Test workflow
199197 """
200198
201- state = FSMField (choices = BlogPostState .choices , default = BlogPostState .NEW , protected = True )
199+ state = fsm . FSMField (choices = BlogPostState .choices , default = BlogPostState .NEW , protected = True )
202200
203201 class Meta :
204202 permissions = [
@@ -274,27 +272,37 @@ class AdminBlogPostState(models.TextChoices):
274272 HIDDEN = "hidden" , "Hidden"
275273
276274
277- class AdminBlogPostStep (models .TextChoices ):
278- STEP_1 = "step1" , "Step one"
279- STEP_2 = "step2" , "Step two"
280- STEP_3 = "step3" , "Step three"
275+ class AdminBlogPostStep (models .IntegerChoices ):
276+ STEP_1 = 1 , "Step one"
277+ STEP_2 = 2 , "Step two"
278+ STEP_3 = 3 , "Step three"
279+
280+
281+ class AdminBlogPostDbState (fsm .FSMModelMixin , models .Model ):
282+ id = models .CharField (primary_key = True )
283+ label = models .CharField ()
284+
285+ def __str__ (self ):
286+ return self .label
281287
282288
283289class AdminBlogPost (fsm .FSMModelMixin , models .Model ):
284290 title = models .CharField (max_length = 50 )
285291
286- state = FSMField (
292+ state = fsm . FSMField (
287293 choices = AdminBlogPostState .choices ,
288294 default = AdminBlogPostState .CREATED ,
289295 protected = True ,
290296 )
291297
292- step = FSMField (
298+ step = fsm . FSMIntegerField (
293299 choices = AdminBlogPostStep .choices ,
294300 default = AdminBlogPostStep .STEP_1 ,
295301 protected = False ,
296302 )
297303
304+ key_state = fsm .FSMKeyField (DbState , on_delete = models .CASCADE , null = True )
305+
298306 # state transitions
299307 def __str__ (self ) -> str :
300308 return f"{ self .title } ({ self .state } )"
0 commit comments