Hi Stuart,

On 02/21/2012 05:29 PM, Stuart Laughlin wrote:
> It seems to me there is a discrepancy between ModelForm and Form when
> it comes to fields with choices. When I create a ModelForm from a
> model having a CharField with choices set, the resultant form contains
> a select with a blank choice included by default. On the other hand,
> when I create a Form and give it a ChoiceField, the resultant form
> contains a select *without* a blank choice included by default.
> 
> In other words, I can't think of why this form field declaration...
> 
> my_form_field = forms.ChoiceField(choices=MY_CHOICES)
> 
> ...shouldn't result in a Select widget with a blank choice included.
> And that goes double if I set required=False.

When you are creating a ChoiceField in a Form directly, you are
specifying exactly what the choices will be *for the form field*. It's
important that you can get full control at some level - if a blank
option were automatically added to ChoiceFields, how would you propose
that a developer remove it if they didn't want it?

A ModelForm, on the other hand, is an automated translation layer from a
Model to a Form, so it has some leeway to make its best guess as to how
you'd want your model represented as a form. In this case, that usually
includes adding a blank option (but not always: if the field is required
and has a default value, no blank option is added). And if you don't
like its "best guess", you always have the option to override things
manually at the Form layer.

In any case, this would be a backwards-incompatible change, so it would
need to be a clearcut bugfix to even be considered. I don't think it's
an improvement at all, let alone a clearcut bugfix.

> I searched for a ticket along these lines but didn't find anything.
> I'm not an experienced django contributor, so I'm posting here hoping
> for some feedback to verify that I'm on the right track (or not), i.e.
> is this something that I ought to create a ticket for (and some tests
> and a patch)?

I think not, but thanks for taking the time to make the proposal!

Carl

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to