In cases,
PUT requests on a REST resource. Not sending a CharField means, do
nothing to that field but sending a blank string is an attempt to set a
field to an empty string.
In a web form, I may want to require a field be present in the POST data
as it is a part of my form but it may be blank/empty. If that field is not
present, this means that the data isn't returning the fields in which I
have set as required.
{{{
>>> from django import forms
>>> forms.CharField(min_length=0).clean("")
django.core.exceptions.ValidationError: [u'This field is required.']
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22739>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_docs: => 0
* resolution: => wontfix
* needs_tests: => 0
* needs_better_patch: => 0
Comment:
I can see your point, but the required check has been specifically written
to fail on an empty string as if it had been missing. Each field defines a
set of `empty_values`, with the default
[https://github.com/django/django/blob/master/django/core/validators.py#L15|
including an empty string]. If the value is in `empty_values`, this
[https://github.com/django/django/blob/master/django/forms/fields.py#L129|
triggers the required error].
I don't really see a way to change this behaviour in Django without
causing numerous other issues. However, a simple workaround would probably
be for you to make your own subclass of `forms.CharField`, in which you
override the `empty_values` attribute to only include `None`. You can then
use that to only get the `ValidationError` when the value was absent
instead of empty. This is not a public API as far as I can find, so it
might change in the future.
Closing as wontfix, but please reopen if you disagree.
--
Ticket URL: <https://code.djangoproject.com/ticket/22739#comment:1>