#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+unsubscr...@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.