So as I recently came to understand, the default
that we specify in a Django field is merely for filling up pre-existing rows, and doesn't function as a "real" default value at the database level. This can be verified by looking up column_default
from the database shell.
If I understand correctly, default
is used by Postgres to put in values when there is no input. If that is so, then we shouldn't be able to create a new object in Django without providing the default value, right?
For ex -
class MyModel(models.Model):
name = models.CharField(max_length=255)
status = models.BooleanField(default=True) # new field added
For the above model, the default
is used for only backfilling the newly added field for pre-existing rows. It doesn't actually set a default at the database level.
column_name | column_default
-------------+----------------
mymodel |
But if that is so, I shouldn't be able to run this query -
MyModel.objects.create(name='test')
since there's no "real" default. But I can, and I don't understand why.
Thank you!