Is some problem in field options 'null'?

14 views
Skip to first unread message

Jinwen

unread,
Aug 26, 2017, 9:19:33 AM8/26/17
to Django users
I read the document about field options 'null'.

It is said that.

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.


Then I try make a field have "null=True", just save it to database.

like that.

>>> from backend.models import Scrapyd as S
>>> s = S()
>>> s.comment
>>> type(s.comment)
<class 'NoneType'>
>>> s.save()
>>> b  = S.objects.all().last()
>>> b
<Scrapyd: Scrapyd object>
>>> b.comment
>>> type(b.comment)
<class 'NoneType'>


What my question is why the value not like the document said is empty string ''?

I am confused.

Melvyn Sopacua

unread,
Aug 31, 2017, 11:59:32 AM8/31/17
to django...@googlegroups.com
The document says to avoid null=True for character fields,
because they both "mean the same thing": there is nothing
in there.

They are however different and this is why Django returns
None:

- Empty character fields are considered a value
- NULL character fields are not

This is important in unique constraints:
Two rows with empty string are forbidden
Two (or more) rows set to NULL are allowed.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1b239340-78d6-4df2-a12d-9531dfe7912d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages