I have a simple model which looks like this:
class Group(models.Model):
name = models.CharField(max_length = 100, blank=False)
I would expect this to throw an integrity error, but it does not:
group = Group() # name is an empty string here
group.save()
How can I make sure that the name variable is set to something non-empty? I.e to make the database reject any attempts to save an empty string?
From the Django docs in this case, your
namewill be stored as an empty string, because thenullfield option is False by default. if you want to define a custom default value, use thedefaultfield option.On this page, you can see that the
blankis not database-related.Update:
You should override the clean function of your model, to have custom validation, so your model def will be:
Or you can replace
ValidationErrorto something else. Then before you callgroup.save()callgroup.full_clean()which will callclean()Other validation related things are here.