Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#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 Vladimir Prudnikov, 12 years ago

Summary: Extend EMPTY_VALUES for form fieldExtend EMPTY_VALUES for the form and|or model field

comment:2 by Aymeric Augustin, 12 years ago

Resolution: wontfix
Status: newclosed

While this may be appropriate for your particular use case, it sounds like a anti-feature to me:

>>> import urllib
>>> urllib.urlencode({"name":"Hello","parent":"None"})       # my parent is actually called "None", anything wrong with that?
'name=Hello&parent=None'

If you need this logic in your app, you may implement it:

  • for a specific field, in your (Model)Form's clean_<field> method
  • for all fields of a model, in your (Model)Form's clean method (by iterating over all fields)
  • for all fields of all models, in the clean method of a mixin class that you'll add to your (Model)Forms

comment:3 by Vladimir Prudnikov, 12 years ago

Cc: v.prudnikov+django@… 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.

Note: See TracTickets for help on using tickets.
Back to Top