1
votes

How to 'hide' all fields in a form based on a model class in django? There is the hidden widget which can be set on the form, but this solution makes me copy all fields from the model into the form.

1

1 Answers

4
votes

Model._meta.get_all_field_names() gives you a list of the names which you can use to build the widget dictionary on a ModelForm.Meta:

class TestForm(forms.ModelForm):
    class Meta:
        model = Test
        widgets = {field_name: forms.HiddenInput() 
                   for field_name in Test._meta.get_all_field_names()}