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

    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


1 Comment

  • Trey

    Reply Reply July 30, 2013

    An very simple solution.

    Since you’re not capturing the fixedfield that you’re deleting you could also use del instead of pop:

    del self.fields[‘fixedfield’]

Leave A Response

* Denotes Required Field