[Answered ]-How to handle state / conditionally modify fields within Django forms

1👍

Try putting the logic in the form instantiation code as so:

class TaskForm(forms.ModelForm):
    class Meta:
        model = Task

    def handle_state(self, *args, **kwargs):
        task = getattr(self, 'instance', None)
        if task:     
            if task.status = Task.ACCEPTED:
                 self.fields['datereceived'].disabled = True
             elif task.status = Task.COMPLETED:
                 ...

    def __init__(self, *args, **kwargs):
        super(TaskForm, self).__init__(*args, **kwargs)
        self.handle_state(*args, **kwargs)      

1👍

you can use finite state machine. django-fsm to handle states of your task. In this you can define the source and target state of every transition. for reference you can see this example. https://distillery.com/blog/building-for-flexibility-using-finite-state-machines-in-django/

Leave a comment