Thursday, August 31, 2017

Re: Is some problem in field options 'null'?

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.



On Sat, Aug 26, 2017 at 2:09 PM, Jinwen <toonowujinwen@gmail.com> wrote:
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.

--
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-users@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

--
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-users@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/CA%2Bgw1GWiq7jR4ptTLp%2Bj%3DLDboXcKvCuNXL_g8vp6YioqmJuOjQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment