Correct. You told Django that you wanted to specify the field yourself
and it believed you. Django cannot know which attributes you intended to
leave out and which you wanted inherited, so it does the only possible
thing and leaves things entirely in your control.
>
> This seems like undesirable behavior. At the very least it's
> surprising,
Well, surprise is in the eye of the participant. As noted above, it
would be surprising to a lot of us if it did pick up the attributes
because it removes control from the user. The current behaviour isn't
illogical.
> and should probably be documented here:
> http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types.
Definitely. Please open a ticket (if one doesn't already exist in the
documentation component for this) and feel free to attach a patch with
some suggested wording.
>
> I'm fairly new to Django, so unfortunately I'm not sure of the right
> way to do this.
If all you want to do is change the widget, you can do that in the
form's __init__ method by updating self.fields["name"].widget, for
example.
Regards,
Malcolm
>
>> Well, surprise is in the eye of the participant. As noted above, it
>> would be surprising to a lot of us if it did pick up the attributes
>> because it removes control from the user. The current behaviour isn't
>> illogical.
>
> Oh, I wasn't suggesting that the user shouldn't be able to override
> those values, I was only talking about what the default behavior
> should be if the user doesn't specify anything. If the user
> instantiates a form field, and doesn't specify what 'required' should
> be (and the field itself doesn't set this value), it just seems far
> more natural to pick up the value the user already specified in the
> model, rather than arbitrarily choosing 'True'.
It isn't choosing arbitrary defaults. When you declare a form field
declaratively in your ModelForm we have to assume you want that field
and everything that comes with it. So, behind the scenes the declared
form field simply overrides what is the field for that particular
field. Therefore the value it uses in-place of the model form field is
the default value for that attribute of that form field you've just
declared as being the replacement.
>
> Part of the reason I think this represents a common use case is that
> these attributes seem fairly orthogonal to the purposes of validating/
> rendering that fields and widgets are used for. That is, I wouldn't
> imagine that it's common that your choice of which field or widget to
> use to represent a model field will affect whether or not that field
> is required, say. Of course, my experience is limited.
I won't argue that you are wrong here, but to accomplish what you have
asked for is going to involve more trickery than it is really worth
(plus being backwards incompatible at this point). There has been
mention of providing a simpler way to overriding widgets for a field
(as that is the common case) without affecting the field. That
particular proposal will go further than what you've brought up.
>
> I don't know enough yet to suggest how this would be implemented, I
> just wanted to throw the idea out there. But unless there's a
> groundswell of support for my position, I guess I'll just file a
> documentation ticket.
No doubt the documentation needs to be revised slightly for this. It
is mentioned, but only as a single sentence that almost makes no
sense ;-)
Please file a ticket for the documentation fix it would be greatly
appreciated. :-)
Brian Rosner
http://oebfare.com
There's also a ticket waiting for check-in implementing this common
usecase: http://code.djangoproject.com/ticket/9223
P.S. Sorry for the shameless plug :-)
Because customizing widgets is a common request, unlike other attributes.
I believe that the consistency you're talking about is not very useful
here. Speaking from my experience overwriting anything other than a
widget is needed rarely and is easily accomplished with a custom 2-line
helper function.