Code

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#17725 closed New feature (wontfix)

Extend EMPTY_VALUES for the form and|or model field

Reported by: 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?

Attachments (0)

Change History (3)

comment:1 Changed 2 years ago by prudnikov

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Extend EMPTY_VALUES for form field to Extend EMPTY_VALUES for the form and|or model field

comment:2 Changed 2 years ago by aaugustin

  • Resolution set to wontfix
  • Status changed from new to closed

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 Changed 2 years ago by prudnikov

  • 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.