Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

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

Open
moseb opened this issue Jul 15, 2019 · 2 comments
Open

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

moseb opened this issue Jul 15, 2019 · 2 comments

Comments

@moseb
Copy link

moseb commented Jul 15, 2019

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!

@kmmbvnr
Copy link
Collaborator

kmmbvnr commented Jul 15, 2019

Yeah, Django-fsm seems lack of notion of initial starting state, so there is no restrictions on it.

That's interesting point for design.

@jordanmkoncz
Copy link

I've used django-fsm in a few projects and also had this question about how I can enforce new objects to be created with a particular status. @moseb your code seems like a decent solution, but it would be great if django-fsm provided a nice way to enforce this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants