Dynamically hide/show Django admin form fields using only HTML attributes. No javascript required. ™️
- Add js file url to your model admin form's media class -
class MyModelForm(ModelForm):
class Media:
js = (
"https://cdn.jsdelivr.net/gh/scientifichackers/django-hideshow@0.0.1/hideshow.js",
)
- Declare HTML attributes on any fields you want -
class MyModelForm(ModelForm):
class Meta:
widgets = {
"some_integer_choice_field": forms.Select(
attrs={
# all hidden by default
"--hideshow-fields": "a1, a2, a3, a4",
# a2, a4 visible when "0" is selected
"--show-on-0": "a2, a4",
# a1, a2 visible when "1" is selected
"--show-on-1": "a1, a2",
}
),
"some_boolean_field": forms.CheckboxInput(
attrs={
"--hideshow-fields": "b1, b2, b3",
# b1, b2 visible if checkbox checked
# b3 visible if checkbox un-checked
"--show-on-checked": "b1, b2",
}
),
}
- See it work -