Django

Code

Ticket #3268 (closed: fixed)

Opened 2 years ago

Last modified 1 year ago

[patch] form_for_model() should use ChoiceField for any DB field with "choices" set

Reported by: brooks.travis@gmail.com Assigned to: adrian
Milestone: Component: Forms
Version: SVN Keywords:
Cc: kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com, real.human@mrmachine.net, espen@grindhaug.org, jm.bugtracking@gmail.com Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

__init__.diff (6.4 kB) - added by ryan.moe@gmail.com on 01/19/07 16:12:04.
models.diff (1.4 kB) - added by ryan.moe@gmail.com on 01/19/07 16:13:17.
select_widget_for_fields_with_choices.diff (8.0 kB) - added by mrmachine on 03/30/07 04:57:32.
updated *Field.formfield() methods, with tests.
choices.diff (0.8 kB) - added by Baptiste <baptiste.goupil@gmail.com> on 04/10/07 11:52:30.
Patch of the #3991, so much simpler (but maybe less powerful, I don't know)

Change History

01/10/07 16:33:48 changed by adrian

  • priority changed from high to normal.
  • severity changed from major to normal.

01/12/07 23:25:07 changed by adrian

  • summary changed from newforms.models.form_for_model() does not print CharField with field option choices=CHOICES_LIST as a <select> item to form_for_model() should use ChoiceField for any DB field with "choices" set.

Agreed -- good catch.

01/19/07 16:12:04 changed by ryan.moe@gmail.com

  • attachment __init__.diff added.

01/19/07 16:13:17 changed by ryan.moe@gmail.com

  • attachment models.diff added.

01/19/07 16:24:20 changed by ryan.moe@gmail.com

  • summary changed from form_for_model() should use ChoiceField for any DB field with "choices" set to [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.

01/19/07 16:31:48 changed by adrian

  • stage changed from Unreviewed to Accepted.

Thanks for the patches. I'll have a look.

01/24/07 15:48:53 changed by anonymous

  • cc set to kilian.cavalotti@lip6.fr.

01/30/07 09:49:07 changed by Gary Wilson <gary.wilson@gmail.com>

  • has_patch set to 1.
  • needs_tests set to 1.

There is also a competing patch in #3401 that alters CharField.formfield() to use ChoiceField for a CharField that has choices.

02/28/07 10:00:20 changed by anonymous

  • cc changed from kilian.cavalotti@lip6.fr to kilian.cavalotti@lip6.fr, larlet@gmail.com.

03/28/07 11:47:12 changed by brosner <brosner@gmail.com>

  • cc changed from kilian.cavalotti@lip6.fr, larlet@gmail.com to kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com.

03/30/07 00:50:18 changed by mrmachine <real dot human at mrmachine dot net>

  • cc changed from kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com to kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com, real.human@mrmachine.net.

03/30/07 04:57:32 changed by mrmachine

  • attachment select_widget_for_fields_with_choices.diff added.

updated *Field.formfield() methods, with tests.

03/30/07 05:07:06 changed by mrmachine

  • needs_tests deleted.

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).

04/02/07 20:18:29 changed by SmileyChris

  • stage changed from Accepted to Ready for checkin.

Looks good

04/10/07 11:52:30 changed by Baptiste <baptiste.goupil@gmail.com>

  • attachment choices.diff added.

Patch of the #3991, so much simpler (but maybe less powerful, I don't know)

04/10/07 16:37:26 changed by brosner <brosner@gmail.com>

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) ;)

04/11/07 08:01:55 changed by Baptiste <baptiste.goupil@gmail.com>

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 !

04/12/07 17:50:39 changed by anonymous

  • cc changed from kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com, real.human@mrmachine.net to kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com, real.human@mrmachine.net, espen@grindhaug.org.

04/23/07 20:51:31 changed by anonymous

  • cc changed from kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com, real.human@mrmachine.net, espen@grindhaug.org to kilian.cavalotti@lip6.fr, larlet@gmail.com, brosner@gmail.com, real.human@mrmachine.net, espen@grindhaug.org, jm.bugtracking@gmail.com.

04/23/07 21:09:45 changed by Jonas

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.

04/23/07 21:16:10 changed by mrmachine

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?

04/23/07 21:27:59 changed by Jonas

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.

04/28/07 09:18:03 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [5119]) Fixed #3268 -- Changed default model formfields to use a select widget when the field has a choices attribute. Based on a patch from mrmachine.


Add/Change #3268 ([patch] form_for_model() should use ChoiceField for any DB field with "choices" set)




Change Properties
Action