#36317: ModelChoiceField cannot be hidden by default
-----------------------------+--------------------------------------
Reporter: FloppyDisco | Type: Bug
Status: new | Component: Forms
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
hello all, I have run into something that feels like a bug to me, but i
could be wrong there may be a reason for this behavior.
I have added a custom action form to my Admin page.
my goal is to default these fields to hidden and only show them when the
associated action is selected.
I have accomplished this with JS, however there is a delay before it is
loaded and there is a small layout shift.
the form contains two fields, the relevant code is below.
{{{
choice = ModelChoiceField(
...
widget=Select(
attrs={
"style": "display: none;"
}
),
)
date = DateField(
...
widget=DateInput(
attrs={
"style": "display: none;",
}
),
)
}}}
the DateInput is appropriately hidden when the template is rendered and
requires no JS to hide the input initially.
however, because the way dropdowns are rendered in html. the
ModelChoiceField is not correctly hidden.
the rendered html basically looks like this:
{{{
<label>
<select style="display: none;" />
<span />
</label>
}}}
the problem is that the `<select>` element is hidden by default and the
`<span>` is what the user actually interacts with.
so the expected behavior is that when `display: none;` is set on the
widget, the widget is hidden.
it would be nice if Django was smart enough to set `display: none;` on all
the associated elements to the widget for this particular case.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36317>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.