mirror of
https://github.com/django/django.git
synced 2025-01-03 06:55:47 +00:00
Fixed #35671 -- Clarified string-based fields behavior when null=False.
This commit is contained in:
parent
0ebed5fa95
commit
ca1318988c
@ -43,13 +43,15 @@ If ``True``, Django will store empty values as ``NULL`` in the database. Default
|
|||||||
is ``False``.
|
is ``False``.
|
||||||
|
|
||||||
Avoid using :attr:`~Field.null` on string-based fields such as
|
Avoid using :attr:`~Field.null` on string-based fields such as
|
||||||
:class:`CharField` and :class:`TextField`. If a string-based field has
|
:class:`CharField` and :class:`TextField`. The Django convention is to use an
|
||||||
``null=True``, that means it has two possible values for "no data": ``NULL``,
|
empty string, not ``NULL``, as the "no data" state for string-based fields. If a
|
||||||
and the empty string. In most cases, it's redundant to have two possible values
|
string-based field has ``null=False``, empty strings can still be saved for "no
|
||||||
for "no data;" the Django convention is to use the empty string, not
|
data". If a string-based field has ``null=True``, that means it has two possible
|
||||||
``NULL``. One exception is when a :class:`CharField` has both ``unique=True``
|
values for "no data": ``NULL``, and the empty string. In most cases, it's
|
||||||
and ``blank=True`` set. In this situation, ``null=True`` is required to avoid
|
redundant to have two possible values for "no data". One exception is when a
|
||||||
unique constraint violations when saving multiple objects with blank values.
|
:class:`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
|
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
|
set ``blank=True`` if you wish to permit empty values in forms, as the
|
||||||
|
Loading…
Reference in New Issue
Block a user