Opened 14 years ago
Last modified 13 years ago
#16328 new Bug
FilePathField should include blank option even when required=True
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | yes |
Description
Because there is no blank option in FilePathField form fields when required=True, it is not possible to save an admin form with blank inlines that contain FilePathFields. Since an empty-string option does not pass the required=True validator, the simplest fix is simply to include a (,'----------') option as the first choice for the field, just as with other ChoiceFields.
Change History (2)
comment:1 by , 14 years ago
Summary: | FilePathField should include blank option even when required=False → FilePathField should include blank option even when required=True |
---|
comment:2 by , 13 years ago
Easy pickings: | unset |
---|---|
Triage Stage: | Unreviewed → Accepted |
Note:
See TracTickets
for help on using tickets.
It took me a bit of time to figure out the problem; here's my analysis.
Django's forms have an (undocumented)
empty_permitted
attribute. When this attribute is set toTrue
, validation is short-circuited (see lines 263-266 in django/forms/forms.py). Formsets need this internal API to display extra forms to add objects, but ignore them if they are submitted unchanged (empty). Specifically, the inlines feature of the admin uses this.However, it isn't possible to submit a formset unchanged when it contains a
FilePathField
. This problem may affect other fields that can't be submitted with an empty value, given the UI (radio buttons, drop-down selects), whenblank=False
. The common point of these fields is that their value must be chosen from a finite set defined by theirchoices
attribute.In my opinion, the proper fix is to render fields as if
blank
wasTrue
whenempty_permitted
isTrue
, probably by setting theirinclude_blank
attribute toTrue
. Thus, the select widget forFilePathField
will contain the blank choice,BLANK_CHOICE_DASH
, as the first item, resolving the problem described originally.Unfortunately, I failed to write a patch for this because I'm not sufficiently familiar with the forms implementation. After searching for all instances of
include_blank
andempty_permitted
, I couldn't bridge the gap between them. This isn't an easy picking after all :)