Reputation: 6807
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?
Upvotes: 2
Views: 1177
Reputation: 3800
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
Upvotes: 2