Opened 9 years ago
Closed 9 years ago
#25193 closed Bug (wontfix)
MultiWidget is_hidden is True if subwidgets are dynamically added during its render function
Reported by: | Meredith Roman | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.7 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
I have a custom MultiWidget that dynamically adds subwidgets during its render function. I use this widget as part of a form builder that lets a user generate a checklist or radio field with as many options as they need. Because I don't know how many subwidgets the user will need, I am unable to set the subwidgets during form initializaiton.
This is how I add subwidgets during the MultiWidget's render function:
def render(self, name, value, attrs=None): if not isinstance(value, list): value = self.decompress(value) self.widgets = [] for option in value: self.widgets.append(forms.TextInput()) return super(OptionsWidget, self).render(name, value, attrs)
This is the way that the MultiWidget is_hidden property is currently set:
@property def is_hidden(self): return all(w.is_hidden for w in self.widgets)
Because I do not have any subwidgets assigned to my widget at form initilaization, and because all() returns True on an empty iterable, is_hidden was set to True for my widget.
I'm currently using this overwritten version of is_hidden for the widget:
@property def is_hidden(self): return False if self.widgets == [] else all(w.is_hidden for w in self.widgets)
Attachments (1)
Change History (3)
by , 9 years ago
Attachment: | MultiWidget_is_hidden_patch.diff added |
---|
comment:1 by , 9 years ago
comment:2 by , 9 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I could imagine not every user would want the behavior you desire, and it seems a bit of an edge case. Therefore, it might be better to continue using your own subclass.