2

I have an extra field in a Django ModelForm. It is a Boolean field and I want it to be set to true if another field, from the model is not null. How do I change the value of the field in the ModelForm's constructor as I don't want to create a ModelForm dynamically?

1 Answer 1

2

Something like this might work for you:

class ModelFormClass(forms.ModelForm):

    boolean_field = forms.BooleanField()

    def __init__(*args, **kwargs):
        super(ModelFormClass, self).__init__(*args, **kwargs)
        if self.instance.pk and not self.instance.field:
            self.fields['boolean_field'].initial = True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.