[Django]-Add non-null and unique field with already populated model

33👍

Unfortunatelly, I found no answer but I could create one solution:

  • First I have created a migration that allows the slug field to be nullable;
  • Then I have created another migration that populates the slug column with proper values to every row in the model;
  • Then another migration which adds the not-null constraint in the column.

2👍

You do your model changes (add field, change, etc), then you call manage.py makemigrations, then apply the migrations with manage.py migrate

You can add the field with null=True, then you e.g. make a script to populate it one time

Otherwise, if you need to populate the field within the migration you can write a custom one, see https://docs.djangoproject.com/en/1.7/ref/migration-operations/#writing-your-own

👤bakkal

Leave a comment