38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from django.db import migrations
|
|
|
|
|
|
def seed_regions(apps, schema_editor):
|
|
region_model = apps.get_model("showcase", "Region")
|
|
seed_path = Path(__file__).with_name("regions.json")
|
|
regions = json.loads(seed_path.read_text())
|
|
|
|
for region in regions["countries"]:
|
|
region_model.objects.update_or_create(
|
|
code=region["code"],
|
|
defaults={"name": region["name"], "level": "country", "parent": None},
|
|
)
|
|
|
|
for region in regions["subdivisions"]:
|
|
region_model.objects.update_or_create(
|
|
code=region["code"],
|
|
defaults={
|
|
"name": region["name"],
|
|
"level": "subdivision",
|
|
"parent_id": region["parentCode"],
|
|
},
|
|
)
|
|
|
|
|
|
def unseed_regions(apps, schema_editor):
|
|
apps.get_model("showcase", "Region").objects.all().delete()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [("showcase", "0001_initial")]
|
|
|
|
operations = [migrations.RunPython(seed_regions, unseed_regions)]
|
|
|