Ticket #4960: potential-solution.patch

File potential-solution.patch, 1.1 KB (added by Daniel Ward, 9 years ago)

Potential solution by adding in 'strip' option to CharField

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 57d6a75..cdbe679 100644
    a b class Field(six.with_metaclass(RenameFieldMethods, object)):  
    210210
    211211
    212212class CharField(Field):
    213     def __init__(self, max_length=None, min_length=None, *args, **kwargs):
    214         self.max_length, self.min_length = max_length, min_length
     213    def __init__(self, max_length=None, min_length=None, strip=False, *args, **kwargs):
     214        self.max_length, self.min_length, self.strip = max_length, min_length, strip
    215215        super(CharField, self).__init__(*args, **kwargs)
    216216        if min_length is not None:
    217217            self.validators.append(validators.MinLengthValidator(int(min_length)))
    class CharField(Field):  
    231231            attrs.update({'maxlength': str(self.max_length)})
    232232        return attrs
    233233
     234    def clean(self, value):
     235        if self.strip and isinstance(value, six.text_type):
     236            value = self.to_python(value)
     237            value = value.strip()
     238        return super(CharField, self).clean(value)
     239
    234240
    235241class IntegerField(Field):
    236242    widget = NumberInput
Back to Top