FileField special-casing breaks MultiValueField including a FileField
|Reported by:||Carl Meyer||Owned by:||Carl Meyer|
|Cc:||mail@…, bradleyayers||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||no|
There are a couple places where FileField is special-cased using
isinstance in the forms code: specifically, in http://code.djangoproject.com/browser/django/trunk/django/forms/forms.py#L280 BaseForm._clean_fields, a FileField's clean method is passed the initial value as well as the data, and in http://code.djangoproject.com/browser/django/trunk/django/forms/forms.py#L438 BoundField.as_widget the initial value is used as the rendered data even when the form is bound.
This handling of FileFields is correct, and makes them behave more naturally with redisplayed bound forms. But, as usual, the use of
isinstance makes things more brittle than necessary. Specifically, in http://bitbucket.org/carljm/django-form-utils django-form-utils I've implemented a ClearableFileField via MultiValueField and MultiWidget (because the ClearableFileField contains both a FileField and a checkbox for clearing it). The isinstance special-casing means the ClearableFileField can't get the right behavior to parallel a FileField (because it's not a FileField subclass, it's a MultiValueField subclass containing a FileField).
It would be better if the special FileField behavior were implemented with a class attribute flag or a polymorphic method on Field, rather than an
isinstance check, so that any field could trigger the appropriate behavior regardless of its inheritance ancestry.
(This is obviously low priority, not expecting anyone else to work on it, just recording it here as a I may find some time to work on a patch).
Change History (17)
comment:1 Changed 6 years ago by
|Patch needs improvement:||unset|