FSMField and django model validation integration #120
Comments
Can you please provide real use-case scenario for this feature? Isn't Model.clean() valid place for this logic? btw, you can try to mark
|
Hi, I was thinking about maybe something like
|
Sorry, by real use case, I mean the specific business scenario. Not the abstract program sample. So, when, in real life you need to have the check on the first status set? Is it so different from situation, when you have Typically, real scenarios could be solved in different ways, like this one. And I need to evaluate it, in order to understand is this required feature for the library or not. |
Hi, As you said before I could do it in the save or clean methods (with a "not self.id" check), I'm not saying this is a must have in django-fsm, just asking if there is a way to manage this initial status check within the django-fsm existing design (that's what I was asking with the "source=None, target='new'" example, could this work?) Thanks to django-fsm I can do without many if/else statements in my code, I'd like to keep it that way :) This "uninitialized"/ "initialized" solution could work but it would create a reservation first and then reject it, which is not the expected behavior, the record should not be created at all. |
-1 As this is too particular to the db layer used and can be solved easily using signals I wouldn't recommend adding this complexity to django-fsm. |
Hi @ashwoods , The project is 6 years old I'm almost sure somebody already had the same question before :) If I understand correctly your recommendation is
which is exactly what django-fsm was designed to avoid in the first place. So I'm a bit confused here, especially with the "-1" ... |
Ok, in total several thoughts that I have.
def validate_me(model, source_state, target_state):
if model.user is None:
raise ValidationError('You need a user assigned!')
class MyModel(models.Model):
state = FSMField(state_validators = {
'source_state': validate_me
}) |
Thanks @kmmbvnr , that's a lot of data to process, let me write some code and see how it works out and I'll come back to you :) You raise some very valid points I hadn't thought about, my initial plan to bury everything in the condition methods has gone rogue..! :( |
This patch handles ValidationErrors coming from `save_model` or `save_related`. This is useful for e.g. django-fsm, where a transition method might throw this. An alternative approach would be to hook into the form's validation (`is_valid`), but this is not as straight forward and it seems like a good behavior to handle ValidationErrors here, instead of letting them bubble up and typically causing a 500 for the admin page. Ref: viewflow/django-fsm#120
FYI: with #120 a ValidationError from the transitioning function would be handled in the admin. |
As for admin integration I came up with gadventures/django-fsm-admin#74 - using a method on the instance.
Why have you decided to not do this after all? |
Hi,
is it possible to create a condition for the object creation with the same syntax that exists for other transitions?
Say I have a default state = FsmField(default='new'), the only way I found to check the initial conditions is to use the clean or save methods and check that the id is null and then run some initial tests.
I would have been nice to be able to do it in the same fashion as all the other state related tests.
Is it possible to do it that way? How would you recommend to implement this using django-fsm?
The text was updated successfully, but these errors were encountered: