Opened 18 years ago
Closed 18 years ago
#3196 closed defect (fixed)
Dynamically setting choices for a ChoiceField is inconsistent
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Forms | Version: | |
Severity: | normal | Keywords: | newforms choicefield |
Cc: | Triage Stage: | Design decision needed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
To get your list of choices to show up in a ChoiceField before a form is submitted, you must set field.widget.choices = your_iterable. Setting field.choices has no effect. To get the field to validate you must set field.choices = your_iterable, not field.widget.choices. This is inconsistent and confusing. It seems to me that setting field.choices should be the correct thing to do for both.
Attachments (1)
Change History (7)
by , 18 years ago
Attachment: | choices_patch.diff added |
---|
comment:1 by , 18 years ago
Summary: | Dynamically setting choices for a ChoiceField is inconsistent → [patch] Dynamically setting choices for a ChoiceField is inconsistent |
---|
The attached patch adds a set_choices method to ChoiceField/MultiChoiceField classes that sets the choices field in the Field and then tells the Widget about it.
comment:2 by , 18 years ago
Summary: | [patch] Dynamically setting choices for a ChoiceField is inconsistent → [design-q] [patch] Dynamically setting choices for a ChoiceField is inconsistent |
---|
The current way it's handled is intentional -- a Field
doesn't know anything about the internals of its Widget
. The Widget.choices
completely defines display, whereas the Field.choices
completely defines valid options. I agree that this can be confusing, but something rubs me the wrong way about having a Field
manipulate the internals of its Widget
.
I'm putting [design-q]
in the ticket summary to designate "a design decision needs to be made here."
comment:3 by , 18 years ago
Summary: | [design-q] [patch] Dynamically setting choices for a ChoiceField is inconsistent → Dynamically setting choices for a ChoiceField is inconsistent |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:4 by , 18 years ago
Patch needs improvement: | set |
---|
OK, I made the design decision. :-)
In [4378], I changed ChoiceField.__init__()
so that a widget uses the choices from its ChoiceField
regardless of whether the widget has its own choices.
The next step, to fix this particular ticket, is to accomodate for users who set choices
on the field after the fact. Rather than use the behavior in this patch (set_choices()
methods), I'd rather we used properties, to be more Pythonic and transparent.
comment:6 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Adds a set_choices method to ChoiceField/MultiChoiceField