#17725 closed New feature (wontfix)
Extend EMPTY_VALUES for the form and|or model field
Reported by: | Vladimir Prudnikov | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | v.prudnikov+django@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
By default any values which is in validators.EMPTY_VALUES
interpreted as an empty value. It would be great to be able to extend EMPTY_VALUES for the field by providing field argument empty_values
which is a list of additional values interpreted as empty.
parent = forms.ModelChoiceField(...., empty_values=["null", "None"])
This is very useful for public API's. Here is an example of how external script can generate POST or GET data which will have different meaning in the Django's form:
>>> import urllib >>> urllib.urlencode({"name":"Hello","parent":None}) 'name=Hello&parent=None'
What do you think?
Change History (3)
comment:1 by , 13 years ago
Summary: | Extend EMPTY_VALUES for form field → Extend EMPTY_VALUES for the form and|or model field |
---|
comment:2 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:3 by , 13 years ago
Cc: | added |
---|
clean_<field> method is not called if you pass "None" into UrlField or EmailField.
REmoving error messages from form.errors manually doens't seems a good solution to me...
>>> urllib.urlencode({"name":"Hello","parent":"None"}) # my parent is actually called "None", anything wrong with that?
If developer has defined string "None" as real None I don't see anything wrong with that.
I think this feature is quite similar anti-feature as validators
attribute because the same way developer can raise an exception when string "None" has passed for parent
, anything wrong with that? Well, may be, but in this case why validators
is not anti-feature and empty_values
is?
I think flexibility is the key.
While this may be appropriate for your particular use case, it sounds like a anti-feature to me:
If you need this logic in your app, you may implement it:
clean_<field>
methodclean
method (by iterating over all fields)clean
method of a mixin class that you'll add to your (Model)Forms