I’ve a model like this:
class Message(models.Model):
msg = models.CharField(max_length = 150)
and I have a form for insert the field. Actually django allows empty spaces, for examples if I inset in the field one space it works. But now I want to fix this: the field is not required, but if a user insert a spaces, the validation should fail.
I’ve added:
class Message(models.Model):
msg = models.CharField(max_length = 150, blank = False)
but it doesn’t work.
What’s the mistake?
Whitespace is not considered to be blank. blank specifically refers to no input (i.e. an empty string
''). You will need to use a model field validator that raises an exception if the value only consists of spaces. See the documentation for details.Example: