'If "null=True", which is stored in DB "NULL" or "empty values"?
I created the django model "User" with "null=True" as shown below:
# "myapp/models.py"
from django.db import models
class User(models.Model): # Here
name = models.CharField(max_length=100, null=True, blank=True)
Then, I intuitively thought "NULL" is stored in DB when an empty value is saved as shown below because of "null=True":
But, when I checked the django documentation about "NULL", it says:
If True, Django will store empty values as NULL in the database.
So, as the documentation says, if "null=True" and an empty value is saved as shown above, is "an empty value" stored in DB instead of "NULL"?
Solution 1:[1]
So, as the documentation says, "empty values" is stored in DB instead of
NULL
ifnull=True
?
No. If you provide an empty value, for example in a ModelForm
leave a form field empty, Django will by default store it as NULL
in the database, so it is the other way around. So it means that for this it will normally not store an empty string, but always NULL
.
Solution 2:[2]
No, if "null=True" and an empty value is saved, "NULL" is stored in DB instead of "an empty value".
For example, there is the django model "User" with "null=True" as shown below:
# "myapp/models.py"
from django.db import models
class User(models.Model): # Here
name = models.CharField(max_length=100, null=True, blank=True)
Then, you saved an empty value:
Now, "NULL" is stored in "myapp_user" table in SQLite as shown below:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Willem Van Onsem |
Solution 2 | Kai - Kazuya Ito |