FAQ
#22224: Non-nullable blank string-based model field validation doesn't prevent or
clean `None`
-----------------------------------------+------------------------
                Reporter: charettes | Owner: nobody
                    Type: Bug | Status: new
               Component: Uncategorized | Version: master
                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 |
-----------------------------------------+------------------------
  Since
  [https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.null
  we discourage the use of NULLable string-based fields] the assumed way of
  holding text that might be empty would be to use the `blank` option.

  In this case I would expect `full_clean` to raise a `ValidationError` when
  an empty non-nullable field is assigned `None`:

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

  class A(models.Model):
      b = models.CharField(blank=True)

  a = A(b=None)
  a.full_clean() # Passes
  a.save() # Integrity error
  }}}

  Unfortunately specifying `blank=True` disable all validation against the
  `b` field, leading to database integrity error upon calling `save()`. The
  obvious solution here would be to override `A.clean` to convert `b` to an
  empty string when it equals `None` but it strikes me as a non trivial
  change for a quite common use case: correctly allowing a string-based
  field to be empty.

  This is not an issue when cleaning data retrieved from the usual
  `application/form-url-encoded` `request.GET` or `request.POST` since an
  empty value is always represented by the empty string. However, in the
  case of an `application/json` encoded payload one may either provide
  `None` or `''` (`null` or `''`) to express emptiness. Which will trigger
  the issue described above.

  Attaching a patch that special case the `empty_values` of non-nullable
  string based fields based on the `empty_strings_allowed` flag. All tests
  pass on Py7/SQLite3.

--
Ticket URL: <https://code.djangoproject.com/ticket/22224>
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 django-updates+unsubscribe@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/052.6886655fe8cd9631a5b5508438bf60e8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Django at Mar 7, 2014 at 7:39 am
    #22224: Non-nullable blank string-based model field validation doesn't prevent or
    clean `None`
    -------------------------------------+-------------------------------------
          Reporter: charettes | Owner: nobody
              Type: Bug | Status: new
         Component: Database layer | Version: master
       (models, ORM) | Resolution:
          Severity: Normal | Triage Stage:
          Keywords: | Unreviewed
         Has patch: 1 | Needs documentation: 0
       Needs tests: 0 | Patch needs improvement: 0
    Easy pickings: 0 | UI/UX: 0
    -------------------------------------+-------------------------------------
    Changes (by charettes):

      * has_patch: 0 => 1
      * component: Uncategorized => Database layer (models, ORM)


    Comment:

      See https://github.com/django/django/pull/2409

    --
    Ticket URL: <https://code.djangoproject.com/ticket/22224#comment:1>
    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 django-updates+unsubscribe@googlegroups.com.
    To post to this group, send email to django-updates@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.02f6a353ee856588741e226d92b34963%40djangoproject.com.
    For more options, visit https://groups.google.com/d/optout.
  • Django at Mar 7, 2014 at 8:00 am
    #22224: Non-nullable blank string-based model field validation doesn't prevent or
    clean `None`
    -------------------------------------+-------------------------------------
          Reporter: charettes | Owner: nobody
              Type: Bug | Status: new
         Component: Database layer | Version: master
       (models, ORM) | Resolution:
          Severity: Normal | Triage Stage:
          Keywords: | Unreviewed
         Has patch: 1 | Needs documentation: 0
       Needs tests: 0 | Patch needs improvement: 0
    Easy pickings: 0 | UI/UX: 0
    -------------------------------------+-------------------------------------
    Description changed by charettes:

    Old description:
    Since
    [https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.null
    we discourage the use of NULLable string-based fields] the assumed way of
    holding text that might be empty would be to use the `blank` option.

    In this case I would expect `full_clean` to raise a `ValidationError`
    when an empty non-nullable field is assigned `None`:

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

    class A(models.Model):
    b = models.CharField(blank=True)

    a = A(b=None)
    a.full_clean() # Passes
    a.save() # Integrity error
    }}}

    Unfortunately specifying `blank=True` disable all validation against the
    `b` field, leading to database integrity error upon calling `save()`. The
    obvious solution here would be to override `A.clean` to convert `b` to an
    empty string when it equals `None` but it strikes me as a non trivial
    change for a quite common use case: correctly allowing a string-based
    field to be empty.

    This is not an issue when cleaning data retrieved from the usual
    `application/form-url-encoded` `request.GET` or `request.POST` since an
    empty value is always represented by the empty string. However, in the
    case of an `application/json` encoded payload one may either provide
    `None` or `''` (`null` or `''`) to express emptiness. Which will trigger
    the issue described above.

    Attaching a patch that special case the `empty_values` of non-nullable
    string based fields based on the `empty_strings_allowed` flag. All tests
    pass on Py7/SQLite3.
    New description:

      Since
      [https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.null
      we discourage the use of NULLable string-based fields] the assumed way of
      holding text that might be empty would be to use the `blank` option.

      In this case I would expect `full_clean` to raise a `ValidationError` when
      an empty non-nullable field is assigned `None`:

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

      class A(models.Model):
          b = models.CharField(blank=True)

      a = A(b=None)
      a.full_clean() # Passes
      a.save() # Integrity error
      }}}

      Unfortunately specifying `blank=True` disable all validation against the
      `b` field, leading to database integrity error upon calling `save()`. The
      obvious solution here would be to override `A.clean` to convert `b` to an
      empty string when it equals `None` but it strikes me as a non trivial
      change for a quite common use case: correctly allowing a string-based
      field to be empty.

      This is not an issue when cleaning data retrieved from the usual
      `application/form-url-encoded` `request.GET` or `request.POST` since an
      empty value is always represented by the empty string. However, in the
      case of an `application/json` encoded payload one may either provide
      `None` or `''` (`null` or `''`) to express emptiness. Which will trigger
      the issue described above.

      Attaching a patch that special case the `empty_values` of non-nullable
      string based fields based on the `empty_strings_allowed` flag. All tests
      pass on Py2.7/SQLite3.

    --

    --
    Ticket URL: <https://code.djangoproject.com/ticket/22224#comment:2>
    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 django-updates+unsubscribe@googlegroups.com.
    To post to this group, send email to django-updates@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.8184d1266a42728a4ecba823062e95c1%40djangoproject.com.
    For more options, visit https://groups.google.com/d/optout.
  • Django at Mar 7, 2014 at 6:18 pm
    #22224: Non-nullable blank string-based model field validation doesn't prevent or
    clean `None`
    -------------------------------------+-------------------------------------
          Reporter: charettes | Owner: nobody
              Type: Bug | Status: new
         Component: Database layer | Version: master
       (models, ORM) | Resolution:
          Severity: Normal | Triage Stage: Accepted
          Keywords: | Needs documentation: 0
         Has patch: 1 | Patch needs improvement: 0
       Needs tests: 0 | UI/UX: 0
    Easy pickings: 0 |
    -------------------------------------+-------------------------------------
    Changes (by claudep):

      * stage: Unreviewed => Accepted


    --
    Ticket URL: <https://code.djangoproject.com/ticket/22224#comment:3>
    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 django-updates+unsubscribe@googlegroups.com.
    To post to this group, send email to django-updates@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.c575e824a75c7fde8c07c2f14233947b%40djangoproject.com.
    For more options, visit https://groups.google.com/d/optout.
  • Django at Mar 7, 2014 at 9:10 pm
    #22224: Non-nullable blank string-based model field validation doesn't prevent or
    clean `None`
    -------------------------------------+-------------------------------------
          Reporter: charettes | Owner: nobody
              Type: Bug | Status: new
         Component: Database layer | Version: master
       (models, ORM) | Resolution:
          Severity: Normal | Triage Stage: Accepted
          Keywords: | Needs documentation: 0
         Has patch: 1 | Patch needs improvement: 0
       Needs tests: 0 | UI/UX: 0
    Easy pickings: 0 |
    -------------------------------------+-------------------------------------

    Comment (by charettes):

      As pointed out by Loïc this issue is related to #20205 which propose to
      clean `''` to `None` for fields disallowing empty strings.

      Maybe the correct solution here would be to clean `None` to `''` for non-
      null fields allowing empty strings?

    --
    Ticket URL: <https://code.djangoproject.com/ticket/22224#comment:4>
    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 django-updates+unsubscribe@googlegroups.com.
    To post to this group, send email to django-updates@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.ec97d3328c24c275a7098c8d1af4e3d9%40djangoproject.com.
    For more options, visit https://groups.google.com/d/optout.
  • Django at Mar 23, 2014 at 1:42 am
    #22224: Non-nullable blank string-based model field validation doesn't prevent or
    clean `None`
    -------------------------------------+-------------------------------------
          Reporter: charettes | Owner: nobody
              Type: Bug | Status: new
         Component: Database layer | Version: master
       (models, ORM) | Resolution:
          Severity: Normal | Triage Stage: Accepted
          Keywords: | Needs documentation: 0
         Has patch: 1 | Patch needs improvement: 0
       Needs tests: 0 | UI/UX: 0
    Easy pickings: 0 |
    -------------------------------------+-------------------------------------

    Comment (by charettes):

      Another solution might be to special case `get_db_prep_value` to convert
      `None` to `''` when the field is not `NULL`'able and
      `empty_strings_allowed`.

      This would be completely backward compatible in the case of a model
      specific `clean()` with a `self.non_null_string_based_field is None`
      conditional.

    --
    Ticket URL: <https://code.djangoproject.com/ticket/22224#comment:5>
    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 django-updates+unsubscribe@googlegroups.com.
    To post to this group, send email to django-updates@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.6bf9b407270cad4a94b0c54708a5f956%40djangoproject.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdjango-updates @
categoriesdjango, python
postedMar 7, '14 at 7:34a
activeMar 23, '14 at 1:42a
posts6
users1
websitedjangoproject.com

1 user in discussion

Django: 6 posts

People

Translate

site design / logo © 2019 Grokbase