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


One thought on “Replace user-input field with enforced value in Django form”

  1. 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 Reply

Your email address will not be published. Required fields are marked *