Opened 18 years ago
Closed 18 years ago
#3268 closed defect (fixed)
[patch] form_for_model() should use ChoiceField for any DB field with "choices" set
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | kilian.cavalotti@…, larlet@…, brosner@…, real.human@…, espen@…, jm.bugtracking@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have a model with a models.CharField(choices=GENDER_CHOICES), but when I run newforms.models.form_for_model() on the model containing that field, and do a print on the resulting form, I get a plain <input type="text">.
Attachments (4)
Change History (23)
comment:1 by , 18 years ago
priority: | high → normal |
---|---|
Severity: | major → normal |
comment:2 by , 18 years ago
Summary: | newforms.models.form_for_model() does not print CharField with field option choices=CHOICES_LIST as a <select> item → form_for_model() should use ChoiceField for any DB field with "choices" set |
---|
by , 18 years ago
Attachment: | __init__.diff added |
---|
by , 18 years ago
Attachment: | models.diff added |
---|
comment:3 by , 18 years ago
Summary: | form_for_model() should use ChoiceField for any DB field with "choices" set → [patch] form_for_model() should use ChoiceField for any DB field with "choices" set |
---|
I wasn't sure which fix would be best so I included both. One fixes this in django.db.models.fields by changing all the *Field.formfield() calls, while the other only changes the form_for_model and for_for_instance calls. Both ways worked for the forms I have where this problem was occurring.
comment:4 by , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Thanks for the patches. I'll have a look.
comment:5 by , 18 years ago
Cc: | added |
---|
comment:6 by , 18 years ago
Has patch: | set |
---|---|
Needs tests: | set |
There is also a competing patch in #3401 that alters CharField.formfield()
to use ChoiceField
for a CharField
that has choices
.
comment:7 by , 18 years ago
Cc: | added |
---|
comment:8 by , 18 years ago
Cc: | added |
---|
comment:9 by , 18 years ago
Cc: | added |
---|
by , 18 years ago
Attachment: | select_widget_for_fields_with_choices.diff added |
---|
updated *Field.formfield() methods, with tests.
comment:10 by , 18 years ago
Needs tests: | unset |
---|
the last two patches were made out of date by recent changes to django/db/models/fields/init.py, so i've re-implemented this functionality by creating an appropriate Select widget (which can still be overridden if a custom widget is passed in directly) for any form fields that have choices.
i left the implementation in django/db/models/fields/init.py so that it would be useful for anyone wanting to return a form field from a model field, not only when generating a form object with the form_for_* methods.
i also updated tests/modeltests/model_forms/models.py by adding an integer field with choices to the models, updating the expected output, and a comment explaining that any fields with choices defined in the model are represented by a select list (same as foreignkeys).
by , 18 years ago
Attachment: | choices.diff added |
---|
Patch of the #3991, so much simpler (but maybe less powerful, I don't know)
comment:12 by , 18 years ago
Baptiste,
Your patch only effects admin display of the widgets. This bug you discovered is an underlying newforms problem that affects form_for_instance and form_for_model. Which is used by newforms-admin (go figure, right) ;)
comment:13 by , 18 years ago
So does the patch of mrmachine since the modified functions are the ones called by form_for_instance & form_for_model, no ?
I think that my patch just factorizes what mrmachine does in its... but I can be wrong !
comment:14 by , 18 years ago
Cc: | added |
---|
comment:15 by , 18 years ago
Cc: | added |
---|
comment:16 by , 18 years ago
just a heads-up that both attached patches don't work for me anymore at least as of rev. 5063. The widgets in newforms-admin remain simple text input fields with either one.
comment:17 by , 18 years ago
i've not used the newforms-admin branch. does it use form_for_model? is this a problem or conflict with that branch, or a problem with this patch? does it still work for your own form_for_models forms?
comment:18 by , 18 years ago
Oh, I'm sorry. Both patches work with newforms-admin, they're just off by a few lines each, so basically it was me being unable to use patch correctly. I should learn to double-check before updating any bugs. :-/ Apologies.
comment:19 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Agreed -- good catch.