Django model blank=False does not work?

前端 未结 2 915
青春惊慌失措
青春惊慌失措 2020-12-18 22:40

I have the following model in Django 1.5:

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

Note that according to htt

相关标签:
2条回答
  • 2020-12-18 23:13

    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 讨论(0)
  • 2020-12-18 23:17

    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.

    0 讨论(0)
提交回复
热议问题