[Solved]-Default value of Django's model doesn't appear in SQL

18👍

When you set a default for a model field in Django, it isn’t included in the SQL schema generated by Django.

As long as you create your model instances using the ORM, then this doesn’t matter, as Django will set the default value. However, if you create model instances using raw SQL, then you do need to make sure you use the default value for the field if required.

There is a ticket 470 for adding default values to the SQL schema, but it has been closed as won’t fix. Remember than default can be a callable as well as a constant. It wouldn’t be possible to add these callables as defaults to the SQL schema.

If you really require the default to be added to the SQL schema, you could run the alter table statement manually, or add it to a migration.

Leave a comment