#28555: Inconsistent empty value for EmailField(blank=True, null=True) due to 
strip
after check for empty value
---------------------------------------+------------------------
               Reporter:  Olav Morken  |          Owner:  nobody
                   Type:  Bug          |         Status:  new
              Component:  Forms        |        Version:  1.11
               Severity:  Normal       |       Keywords:
           Triage Stage:  Unreviewed   |      Has patch:  0
    Needs documentation:  0            |    Needs tests:  0
Patch needs improvement:  0            |  Easy pickings:  0
                  UI/UX:  0            |
---------------------------------------+------------------------
 Hi,

 I just noticed that if I enter a single space in an EmailField in a
 ModelForm where the backing model field is {{{EmailField(blank=True,
 null=True)}}}, the value is stored as an empty string. On the other hand,
 if I don't enter anything in the field, it is stored as a {{{None}}}
 value.

 Looking at the code
 
(https://github.com/django/django/blob/1.11.4/django/forms/fields.py#L234-L241),
 I think the cause is that it first checks for empty values, and then
 strips the string.

 To reproduce this behavior, try something like:

 {{{#!python
 # models.py
 from django.db import models

 class SomeUser(models.Model):
     address = models.EmailField(null=True, blank=True)


 # views.py
 import pprint
 from django import forms
 from django.http import HttpResponse
 from django.views.decorators.csrf import csrf_exempt
 from .models import SomeUser

 class SomeUserForm(forms.ModelForm):
     class Meta:
         fields = [ 'address' ]
         model = SomeUser

 @csrf_exempt
 def test_view(request):
     form = SomeUserForm(request.POST)
     some_user = form.save()
     return HttpResponse(pprint.pformat(some_user.address) + "\n")
 }}}

 Testing this with {{{curl}}} gives something like this:
 {{{
 $ curl -XPOST -d address= http://127.0.0.1:1253/test
 None
 curl -XPOST -d address=%20 http://127.0.0.1:1253/test
 u''
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28555>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.6511dcb2119db6320836d339c4ee9b56%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to