[Django] #36317: ModelChoiceField cannot be hidden by default

6 views
Skip to first unread message

Django

unread,
Apr 10, 2025, 1:50:06 PM4/10/25
to django-...@googlegroups.com
#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.

Django

unread,
Apr 10, 2025, 3:53:23 PM4/10/25
to django-...@googlegroups.com
#36317: ModelChoiceField cannot be hidden by default
-------------------------------+--------------------------------------
Reporter: Josh Piasecki | Owner: (none)
Type: Bug | Status: closed
Component: Forms | Version: 5.2
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* resolution: => invalid
* status: new => closed

Comment:

Hello Josh Piasecki, thank you for your ticket!

I believe that the behavior you describe derives from the structure of the
rendered HTML and how CSS `display: none;` affects different elements.
While it may seem like a bug, it aligns with standard HTML and CSS
behavior. Given this, and given that the Django ticket tracker is best
suited for confirmed or reproducible issues within Django itself, I'm
afraid I have to close this as `invalid`.

This particular scenario appears to be more of a usage question or a
request for implementation advice. Therefore, it would be more appropriate
to discuss this on the [https://forum.djangoproject.com/c/users/forms-
formsets/18 Django Forum], where community members can provide guidance
and potential solutions. If, through community discussion, this is
identified as a potential improvement for Django, we can revisit and
consider reopening the ticket. Thank you for your understanding.
--
Ticket URL: <https://code.djangoproject.com/ticket/36317#comment:1>
Reply all
Reply to author
Forward
0 new messages