﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31072	Empty string validator not run on nullable charfield	thenewguy	nobody	"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()

}}}"	Uncategorized	closed	Uncategorized	3.0	Normal	invalid			Unreviewed	0	0	0	0	0	0
