[Django]-Django model blank=False does not work?

17đź‘Ť

âś…

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

0đź‘Ť

Django creates your user with an empty string. You can actually run Person.objects.all() and it will give you a list, if you save that to a variable called user_list and do something like user_list[0], it will return a user object with an empty string. I do not know how or why it does this.

Leave a comment