About Making All Form Fields Hidden

You can change the fields of a form to hidden fields by overriding the __init__ method and changing the widgets for all fields set at self.fields:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django import forms
from .models import Project

class ProjectForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            if isinstance(field, (
                forms.ModelMultipleChoiceField,
                forms.MultipleChoiceField,
            )):
                field.widget = forms.MultipleHiddenInput()
            else:
                field.widget = forms.HiddenInput()

    class Meta:
        model = Project
        fields = "__all__"

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2