mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-21 11:31:09 -05:00
here we go
This commit is contained in:
67
parks/migrations/0013_fix_null_locations.py
Normal file
67
parks/migrations/0013_fix_null_locations.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from django.db import migrations
|
||||
|
||||
def fix_null_locations(apps, schema_editor):
|
||||
Park = apps.get_model('parks', 'Park')
|
||||
Country = apps.get_model('cities_light', 'Country')
|
||||
Region = apps.get_model('cities_light', 'Region')
|
||||
City = apps.get_model('cities_light', 'City')
|
||||
|
||||
# Get or create default locations
|
||||
default_country = Country.objects.first()
|
||||
if not default_country:
|
||||
default_country = Country.objects.create(
|
||||
name='Unknown',
|
||||
name_ascii='Unknown',
|
||||
slug='unknown',
|
||||
geoname_id=0,
|
||||
alternate_names='',
|
||||
search_names='Unknown'
|
||||
)
|
||||
|
||||
default_region = Region.objects.filter(country=default_country).first()
|
||||
if not default_region:
|
||||
default_region = Region.objects.create(
|
||||
name='Unknown',
|
||||
name_ascii='Unknown',
|
||||
slug='unknown',
|
||||
geoname_id=0,
|
||||
alternate_names='',
|
||||
country=default_country,
|
||||
display_name='Unknown',
|
||||
search_names='Unknown'
|
||||
)
|
||||
|
||||
default_city = City.objects.filter(region=default_region).first()
|
||||
if not default_city:
|
||||
default_city = City.objects.create(
|
||||
name='Unknown',
|
||||
name_ascii='Unknown',
|
||||
slug='unknown',
|
||||
geoname_id=0,
|
||||
alternate_names='',
|
||||
region=default_region,
|
||||
country=default_country,
|
||||
display_name='Unknown',
|
||||
search_names='Unknown',
|
||||
latitude=0,
|
||||
longitude=0,
|
||||
population=0
|
||||
)
|
||||
|
||||
# Update parks with null locations
|
||||
for park in Park.objects.filter(country__isnull=True):
|
||||
park.country = default_country
|
||||
park.region = default_region
|
||||
park.city = default_city
|
||||
park.location = 'Unknown, Unknown, Unknown'
|
||||
park.save()
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('parks', '0012_merge_20241031_1635'),
|
||||
('cities_light', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(fix_null_locations, reverse_code=migrations.RunPython.noop),
|
||||
]
|
||||
Reference in New Issue
Block a user