Django model blank=False does not work

djangodjango-models

I have the following model in Django 1.5:

class Person(models.Model):
    name = models.CharField(max_length=50)

Note that according to https://docs.djangoproject.com/en/dev/ref/models/fields/
name.blank is by default False which means it must be specified.

However, I could successfully create a Person object as follows:

Person.objects.create()

Notice the name is not specified. What is going on?

Ok, the answer from the docs is :

Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.

Another catch:

Note that validators will not be run automatically when you save a model, but if you are using a ModelForm, it will run your validators on any fields that are included in your form.

It's your responsibility to call the clean methods before saving if you're not using a form.

Best Answer

blank only applies to form field validation as in the admin, django forms, etc.
null on the other hand is a database level nullable column.

As for why blank results in a default '', I had really just accepted it as "that's the way it works" but here's where it appears to be in django.db.models.Field

  def get_default(self):
        """
        Returns the default value for this field.
        """
        if self.has_default():
            if callable(self.default):
                return self.default()
            return force_unicode(self.default, strings_only=True)
        if (not self.empty_strings_allowed or (self.null and
                   not connection.features.interprets_empty_strings_as_nulls)):
            return None
        return ""  
        #       ^ this
Related Topic