Reputation: 1
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"?
Upvotes: 2
Views: 1267
Reputation: 1
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:
Upvotes: 0
Reputation: 476967
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
.
Upvotes: 2