Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django & python-statemachine #348

Closed
enilsen16 opened this issue Feb 21, 2023 · 4 comments
Closed

Django & python-statemachine #348

enilsen16 opened this issue Feb 21, 2023 · 4 comments
Labels

Comments

@enilsen16
Copy link

enilsen16 commented Feb 21, 2023

  • Python State Machine version: 1.0.3
  • Python version: 3.8.X
  • Operating System: OSX

Description

Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.

I'm writing a statemachine to track the state of a given entity and was hoping to use the mixin support for django models to make saving that state a little bit easier. However when I try to add something like this assuming my model is set up correct to use MyStatemachine,

class MyStatemachine(Statemachine):
    # defined states.....

   def __init__(self, value: <a value passed to the class>):
       self.value = value
       super.__init__()

   # conditional for a state
   def is_true():
      return self.value

__init__() got an unexpected keyword argument 'state_field'

It doesn't seem to work with how the Mixin sets things up automatically. I'm wondering if you have any suggestions on the best way to do this?

@enilsen16
Copy link
Author

Just saw that there is discussions in GH now, lmk if you'd rather have me ask there. Thanks!

@fgmacedo
Copy link
Owner

Hi @enilsen16 ,

This state_field constructor parameter is required in order to the state machine knows the Django's model field that holds the state value.

Can you provide more context on why you're passing this value? Understanding the underlying needs may help to suggest alternative implementation.

The Mixin creates an instance of the State machine automatically, and passes the model instance as the target object that will hold the state value, and the model's state field. As this is done automatically for you, you'll have a hard time to change the initialization behavior.

See how the mixin creates a StateMachine instance: https://github.com/fgmacedo/python-statemachine/blob/develop/statemachine/mixins.py#L38

            machine_cls(selfstate_field=self.state_field_name),

So, trying to actually answer your question, you don't need to pass an additional model value to the state machine to use as condition, all model attributes, properties and methods are available by default.

See this example: https://python-statemachine.readthedocs.io/en/latest/auto_examples/order_control_rich_model_machine.html#sphx-glr-auto-examples-order-control-rich-model-machine-py

The SM uses payment_received, that is an attribute of the model.

process_order = processing.to(shipping, cond="payment_received")

This is possible due to the v1.0 new observer implementation: https://python-statemachine.readthedocs.io/en/latest/observers.html

The model is registered as an observer, so you can implement callbacks, conditions, virtually any state/transition hook on the model.

Hope that this helps, let me know if works for you so we can close the issue.

Best regards!

@fgmacedo
Copy link
Owner

fgmacedo commented Feb 21, 2023

@fgmacedo
Copy link
Owner

Closing as an alternative implementation using the model without the need to pass additional parameters to the SM constructor. Docs improved at #352.

Feel free to reopen if something is missing.

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

No branches or pull requests

2 participants