#31072: Empty string validator not run on nullable charfield
-----------------------------------------+------------------------
Reporter: thenewguy | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
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 |
-----------------------------------------+------------------------
I need to store a CharField as null for unique together. I've hit an
oddity attempting to prevent empty strings from passing validation.
I would expect for `test_cannot_create_for_session_with_empty_string()`
below to pass. It fails. However,
`test_cannot_create_altfoo_for_session_with_empty_string()` does pass.
{{{
def validate_truthy_or_null(value):
if not value and value is not None:
raise ValidationError('"%(value)s" was not truthy or null',
params={'value': value})
class AbstractFoo(models.Model):
class Meta:
abstract = True
session = models.CharField(
max_length=255,
null=True,
blank=True,
default=None,
editable=False,
validators=[validate_truthy_or_null],
)
class Foo(AbstractFoo):
pass
class AltFoo(AbstractFoo)
def clean(self):
validate_truthy_or_null(self.session)
class DemonstrationTests(TestCase):
def test_validate_truthy_or_null(self):
for value in ('', 0, False):
with self.subTest(value=value):
with self.assertRaises(ValidationError):
validate_truthy_or_null(value)
def test_cannot_create_for_session_with_empty_string(self):
foo = Foo()
foo.session = ''
with self.assertRaises(ValidationError):
foo.full_clean()
def test_cannot_create_altfoo_for_session_with_empty_string(self):
alt = AltFoo()
alt.session = ''
with self.assertRaises(ValidationError):
alt.full_clean()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31072>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.f4086cf6840b5efa562e05578f5d3b8b%40djangoproject.com.