Search code examples
djangodjango-1.7

How to set the default value for existing entries when adding a new column?


I have a model in my Django app that requires a new BooleanFieldcolumn to be added. This column should never be null - instead, I'd like all the existing entries to set this field to False.

How do I do that? If I just add the new field and set default = False, it'll cause an OperationalError: no such column error.


Solution

  • You need to use Django's migrations, which will allow you to add the field to the database.

    In short, you make the change to your model, create a migration with manage.py makemigrations, and then run it with manage.py migrate.