Coder Perfect

Change a Django form field’s visibility to hidden.

Problem

I have a RegexField on a Django form, which is very similar to a regular text input field.

In my opinion, I should hide it from the user in specific circumstances while attempting to keep the form as comparable as feasible. What’s the most efficient approach to make this field a HiddenInput field?

I’m aware that I may modify the field’s characteristics using:

form['fieldname'].field.widget.attr['readonly'] = 'readonly'

I may also use the following command to set the desired initial value:

form.initial['fieldname'] = 'mydesiredvalue'

However, this will have no effect on the widget’s appearance.

What is the best / most “django-y” / least “hacky” approach to make this field a “hidden” input type?

Asked by Amandasaurus

Solution #1

This could also be beneficial: form.field.as hidden is a property that indicates whether or not a field is hidden.

Answered by semente

Solution #2

If you have a custom template and view, you can remove the field and access the value with modelform.instance.field.

You might also opt to use the following in the view:

form.fields['field_name'].widget = forms.HiddenInput()

I’m not sure if it will protect the save method on the post, though.

Hope it helps.

Answered by christophe31

Solution #3

Defining the field in the initial for was an alternative that worked for me.

forms.CharField(widget = forms.HiddenInput(), required = False)

Then it will stay in place when you override it in the new Class.

Answered by Shay Rybak

Solution #4

To begin with, if you don’t want the user to change the data, it seems simpler to simply remove the field. By making it a hidden field, you’re merely sending additional data over the wire and inviting a malicious user to change it when you don’t want them to. You can supply a keyword parameter to the modelform’s constructor if you have a compelling reason to include the field but hide it. Perhaps something like this:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
    def __init__(self, *args, **kwargs):
        from django.forms.widgets import HiddenInput
        hide_condition = kwargs.pop('hide_condition',None)
        super(MyModelForm, self).__init__(*args, **kwargs)
        if hide_condition:
            self.fields['fieldname'].widget = HiddenInput()
            # or alternately:  del self.fields['fieldname']  to remove it from the form altogether.

Then, in your opinion:

form = MyModelForm(hide_condition=True)

This approach is preferable than altering the modelform’s internals in the view, but it’s a personal preference.

Answered by rych

Solution #5

For normal form you can do

class MyModelForm(forms.ModelForm):
    slug = forms.CharField(widget=forms.HiddenInput())

You can do the following if you have model form.

class MyModelForm(forms.ModelForm):
    class Meta:
        model = TagStatus
        fields = ('slug', 'ext')
        widgets = {'slug': forms.HiddenInput()}

The __init__ method can also be overridden.

class Myform(forms.Form):
    def __init__(self, *args, **kwargs):
        super(Myform, self).__init__(*args, **kwargs)
        self.fields['slug'].widget = forms.HiddenInput()

Answered by anjaneyulubatta505

Post is based on https://stackoverflow.com/questions/6862250/change-a-django-form-field-to-a-hidden-field