from django.db import migrations def backfill_owner(apps, schema_editor): User = apps.get_model("auth", "User") Artifact = apps.get_model("artifacts", "Artifact") first_superuser = User.objects.filter(is_superuser=True).order_by("date_joined").first() if first_superuser is None: return Artifact.objects.filter(owner__isnull=True).update(owner=first_superuser) def noop_reverse(apps, schema_editor): pass class Migration(migrations.Migration): dependencies = [ ("artifacts", "0005_add_owner_nullable"), ] operations = [ migrations.RunPython(backfill_owner, noop_reverse), ]