Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Enforce creation of objects with initial/default state? #218

@moseb

Description

@moseb

Hi!

I have an FSM that looks like this

[review] -> active <--> paused 
   |           |           |
   |           v           |
   \------> deleted <------/

review is the initial state.

Now I noticed, that both .save and __init__ of my model allow direct creation with states that are not review, not the default/initial state. There is custom code similar to this, to enforce that behavior:

    def _deny_creation_with_non_initial_state(self):
        if self.pk is None and self.status == ............:
            raise TransitionNotAllowed()

    def save(self, **kwargs):
        self._deny_creation_with_non_initial_state()
        super().save(**kwargs)

I have a feeling though that my approach is more complicated than necessary. Is there a way to add transitions from before existence to object creation a la source=None, target='review' or so? Any ideas?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions