1👍
✅
You have to do the migration in 3 steps:
-
Add your
UUIDField
withnull=True
so existing rows do not break the constraint. -
Create a data migration to fill the uuids of existing rows similar to your current code.
-
Add another migration with the NOT NULL constraint by removing the
null=True
from your field declaration.
PS: Your code is for the outdated South migrations. The current equivalent for django-migrations would be:
def forwards(apps, schema_editor):
MyClass = apps.get_model('myapp', 'MyClass')
for item in MyClass.objects.all():
# [...]
Source:stackexchange.com