Django 为什么只有基于字符串的字段不能有 null = true?

Django why only string-based field can't have null = true?

If True, Django will store empty values as NULL in the database. Default is False.

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL. One exception is when a CharField has both unique=True and blank=True set. In this situation, null=True is required to avoid unique constraint violations when saving multiple objects with blank values.
For both string-based and non-string-based fields, you will also need to set blank=True if you wish to permit empty values in forms, as the null parameter only affects database storage (see blank).

-- 来自 django/docs --

我明白为什么基于字符串的字段不允许使用 null =true ,但是 Integerfield 或其他字段呢?他们也可以 "two possible values for 'nodata' ",对吧?

并且我了解了 Null 和空字符串之间的区别。为什么 Django 不允许对基于字符串的字段同时使用 null 和 blank?为什么我们应该将 NULL 和空字符串都视为无数据?

Django 允许在基于字符串的字段中使用NULL,建议避免使用。换句话说,Django 作为一种约定建议使用 '' 来表示基于字符串的字段的空值。

此建议的原因是为了避免冗余,因为在大多数情况下,两个值(NULL'')对于基于字符串的字段表示相同的内容。

现在,如果在您的情况下 NULL'' 代表不同的东西,那么您应该使用 NULL 基于字符串的字段。

所以,不要混淆 "Allow" 和 "Suggestion"。第一个是限制,另一个是好的做法。