from django.db import migrations from locations.seed import seed def seed_locations(apps, schema_editor): seed(apps.get_model("locations", "Atoll"), apps.get_model("locations", "Island")) def unseed_locations(apps, schema_editor): """Only removes rows nothing references.""" Atoll = apps.get_model("locations", "Atoll") Island = apps.get_model("locations", "Island") from locations.seed import ATOLLS for entry in ATOLLS: Island.objects.filter( atoll__name=entry["name"], name__in=entry["islands"], users__isnull=True ).delete() Atoll.objects.filter( name=entry["name"], islands__isnull=True, users__isnull=True ).delete() class Migration(migrations.Migration): dependencies = [ ("locations", "0001_initial"), # Islands/atolls are referenced by users; keep the tables in step. ("users", "0001_initial"), ] operations = [migrations.RunPython(seed_locations, unseed_locations)]