Replace user-input field with enforced value in Django form
I’m subclassing someone else’s form; I need to remove their input field and replace it with a fixed value.
class MyForm(TheirForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# remove unwanted field
self.fields.pop('fixedfield')
def clean(self, *args, **kwargs):
cleaned_data = super(MyForm, self).clean(*args, **kwargs)
# Restore field with fixed value
cleaned_data['fixedfield'] = FIXEDVALUE
return cleaned_data
Simples…