You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -378,7 +388,7 @@ class BlogPostWithIntegerField(models.Model):
378
388
source=BlogPostStateEnum.NEW,
379
389
target=BlogPostStateEnum.PUBLISHED,
380
390
)
381
-
defpublish(self):
391
+
defpublish(self, **kwargs):
382
392
pass
383
393
```
384
394
@@ -420,20 +430,14 @@ rollback of all changes executed in an inconsistent state.
420
430
421
431
## Admin Integration
422
432
423
-
1. Make sure `django_fsm` is in your `INSTALLED_APPS` settings:
433
+
> NB: If you're migrating from [django-fsm-admin](https://github.com/gadventures/django-fsm-admin) (or any alternative), make sure it's not installed anymore to avoid installing the old django-fsm.
NB: If you're migrating from [django-fsm-admin](https://github.com/gadventures/django-fsm-admin) (or any alternative), make sure it's not installed anymore to avoid installing the old django-fsm.
434
-
435
-
436
-
2. In your admin.py file, use FSMTransitionMixin to add behaviour to your ModelAdmin. FSMTransitionMixin should be before ModelAdmin, the order is important.
440
+
1. In your admin.py file, use FSMTransitionMixin to add behaviour to your ModelAdmin. FSMTransitionMixin should be before ModelAdmin, the order is important.
437
441
438
442
```python
439
443
from django_fsm.admin import FSMTransitionMixin
@@ -444,34 +448,37 @@ class MyAdmin(FSMTransitionMixin, admin.ModelAdmin):
444
448
...
445
449
```
446
450
447
-
3. You can customize the label by adding ``custom={"label": "My awesome transition"}`` to the transition decorator
451
+
2. You can customize the button by adding `label` and `short_description` to the `custom` attribute of the transition decorator
448
452
449
453
```python
450
454
@transition(
451
455
field='state',
452
456
source=['startstate'],
453
457
target='finalstate',
454
-
custom={"label": False},
458
+
custom={
459
+
"label": "My awesome transition", # this
460
+
"short_description": "Rename blog post", # and this
461
+
},
455
462
)
456
-
defdo_something(self, param):
463
+
defdo_something(self, **kwargs):
457
464
...
458
465
```
459
466
460
-
4.By adding ``custom={"admin": False}`` to the transition decorator, one can disallow a transition to show up in the admin interface.
467
+
4.Hiding a transition is possible by adding ``custom={"admin": False}`` to the transition decorator:
461
468
462
469
```python
463
470
@transition(
464
471
field='state',
465
472
source=['startstate'],
466
473
target='finalstate',
467
-
custom={"admin": False},
474
+
custom={"admin": False},# this
468
475
)
469
-
defdo_something(self, param):
476
+
defdo_something(self, **kwargs):
470
477
# will not add a button "Do Something" to your admin model interface
471
478
```
472
479
473
-
By adding `FSM_ADMIN_FORCE_PERMIT = True` to your configuration settings (or `default_disallow_transition = False` to your admin), the above restriction becomes the default.
474
-
Then one must explicitly allow that a transition method shows up in the admin interface.
480
+
NB: By adding `FSM_ADMIN_FORCE_PERMIT = True` to your configuration settings (or `default_disallow_transition = False` to your admin), the above restriction becomes the default.
481
+
Then one must explicitly allow that a transition method shows up in the admin interface using `custom={"admin": True}`
475
482
476
483
```python
477
484
@admin.register(AdminBlogPost)
@@ -480,6 +487,48 @@ class MyAdmin(FSMTransitionMixin, admin.ModelAdmin):
480
487
...
481
488
```
482
489
490
+
### Custom Forms
491
+
492
+
You can attach a custom form to a transition so the admin prompts for input
493
+
before the transition runs. Add a `form` entry to `custom` on the transition.
494
+
It can be a `forms.Form`/`forms.ModelForm` class or a dotted import path.
495
+
496
+
```python
497
+
from django import forms
498
+
from django_fsm import transition
499
+
500
+
classRenameForm(forms.Form): # DO NOT USE ModelForm
501
+
new_title = forms.CharField(max_length=255)
502
+
503
+
classBlogPost(models.Model):
504
+
title = models.CharField(max_length=255)
505
+
state = FSMField(default="created")
506
+
507
+
@transition(
508
+
field=state,
509
+
source="*",
510
+
target="created",
511
+
custom={
512
+
"label": "Rename",
513
+
"short_description": "Rename blog post",
514
+
"form": "path.to.RenameForm",
515
+
},
516
+
)
517
+
defrename(self, new_title, **kwargs):
518
+
self.title = new_title
519
+
```
520
+
521
+
Behavior details:
522
+
523
+
- When `form` is set, the transition button redirects to a form view instead of
524
+
executing immediately.
525
+
- On submit, `cleaned_data` is passed to the transition method as keyword
526
+
arguments and the object is saved.
527
+
-`RenameForm` receives the current instance automatically.
528
+
- You can override the transition form template by setting
529
+
`fsm_transition_form_template` on your `ModelAdmin` (or override globally `templates/django_fsm/fsm_admin_transition_form.html`).
530
+
531
+
483
532
## Drawing transitions
484
533
485
534
Render a graphical overview of your model transitions.
0 commit comments