I noticed that, for the most part, Django does not enforce default values on the database level.
Field definition in model:
description = models.TextField(default='')
SQL:
description | text | not null
If I add a row using raw SQL (as described here: https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly), and don’t include a value for a field that has a default, will the default still be used?
No.
At least, not based on a test I just did.
models.py:
And then I rand this code:
If Django entered default values for you, it would have sent a value of “U” for
how_sentbut instead I got an error.Then if we create a record using Django,
this is the corresponding SQL:
So if you are really asking whether default values will be entered for you if you use something like
cursor.execute("INSERT INTO...")then the answer is no, it will not set up those default values for you. As you pointed out, Django does not set up the default values for you when creating your database schema, it merely sets up those default values when performing a normal save.