From 818f733bb3cbd52229c8be8b25c50d6ee704bfac Mon Sep 17 00:00:00 2001 From: NABIH Abderrahim Date: Fri, 7 Jun 2024 21:15:37 +0100 Subject: [PATCH 1/2] switching foreign key to a more universal and unique id which is geoname_id as local id would mess up the related data on other models if database repopulation --- src/cities_light/abstract_models.py | 12 ++++++------ src/cities_light/management/commands/cities_light.py | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/cities_light/abstract_models.py b/src/cities_light/abstract_models.py index 89457fc0..11eaaaa6 100644 --- a/src/cities_light/abstract_models.py +++ b/src/cities_light/abstract_models.py @@ -128,7 +128,7 @@ class AbstractRegion(Base): db_index=True) country = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Country', - on_delete=models.CASCADE) + on_delete=models.CASCADE, to_field='geoname_id') class Meta(Base.Meta): unique_together = (('country', 'name'), ('country', 'slug')) @@ -150,10 +150,10 @@ class AbstractSubRegion(Base): db_index=True) country = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Country', - on_delete=models.CASCADE) + on_delete=models.CASCADE, to_field='geoname_id') region = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Region', null=True, blank=True, - on_delete=models.CASCADE) + on_delete=models.CASCADE, to_field='geoname_id') class Meta(Base.Meta): verbose_name = _('SubRegion') @@ -191,11 +191,11 @@ class AbstractCity(Base): subregion = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.SubRegion', blank=True, null=True, - on_delete=models.CASCADE) + on_delete=models.CASCADE, to_field='geoname_id') region = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Region', blank=True, - null=True, on_delete=models.CASCADE) + null=True, on_delete=models.CASCADE, to_field='geoname_id') country = models.ForeignKey(CITIES_LIGHT_APP_NAME + '.Country', - on_delete=models.CASCADE) + on_delete=models.CASCADE, to_field='geoname_id') population = models.BigIntegerField(null=True, blank=True, db_index=True) feature_code = models.CharField(max_length=10, null=True, blank=True, db_index=True) diff --git a/src/cities_light/management/commands/cities_light.py b/src/cities_light/management/commands/cities_light.py index e9907404..9fb30178 100644 --- a/src/cities_light/management/commands/cities_light.py +++ b/src/cities_light/management/commands/cities_light.py @@ -268,7 +268,7 @@ def _get_country_id(self, country_code2): """ if country_code2 not in self._country_codes: self._country_codes[country_code2] = \ - Country.objects.get(code2=country_code2).pk + Country.objects.get(code2=country_code2).geoname_id return self._country_codes[country_code2] @@ -279,7 +279,7 @@ def _get_region_id(self, country_code2, region_id): country_id = self._get_country_id(country_code2) if region_id not in self._region_codes[country_id]: self._region_codes[country_id][region_id] = Region.objects.get( - country_id=country_id, geoname_code=region_id).pk + country_id=country_id, geoname_code=region_id).geoname_id return self._region_codes[country_id][region_id] @@ -291,13 +291,13 @@ def _get_subregion_id(self, country_code2, region_id, subregion_id): country_id = self._get_country_id(country_code2) if region_id not in self._region_codes[country_id]: self._region_codes[country_id][region_id] = Region.objects.get( - country_id=country_id, geoname_code=region_id).pk + country_id=country_id, geoname_code=region_id).geoname_id if subregion_id not in self._subregion_codes[country_id][region_id]: self._subregion_codes[country_id][region_id][subregion_id] = \ SubRegion.objects.get( region_id=self._region_codes[country_id][region_id], - geoname_code=subregion_id).pk + geoname_code=subregion_id).geoname_id return self._subregion_codes[country_id][region_id][subregion_id] def country_import(self, items): From 6d1d135d12c2f431a385db09542e86b55c88be9b Mon Sep 17 00:00:00 2001 From: NABIH Abderrahim Date: Sun, 9 Jun 2024 23:31:49 +0100 Subject: [PATCH 2/2] migration --- ...city_country_alter_city_region_and_more.py | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/cities_light/migrations/0012_alter_city_country_alter_city_region_and_more.py diff --git a/src/cities_light/migrations/0012_alter_city_country_alter_city_region_and_more.py b/src/cities_light/migrations/0012_alter_city_country_alter_city_region_and_more.py new file mode 100644 index 00000000..4d5aa9a8 --- /dev/null +++ b/src/cities_light/migrations/0012_alter_city_country_alter_city_region_and_more.py @@ -0,0 +1,74 @@ +# Generated by Django 5.0.6 on 2024-06-09 22:26 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("cities_light", "0011_alter_city_country_alter_city_region_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="city", + name="country", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="cities_light.country", + to_field="geoname_id", + ), + ), + migrations.AlterField( + model_name="city", + name="region", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="cities_light.region", + to_field="geoname_id", + ), + ), + migrations.AlterField( + model_name="city", + name="subregion", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="cities_light.subregion", + to_field="geoname_id", + ), + ), + migrations.AlterField( + model_name="region", + name="country", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="cities_light.country", + to_field="geoname_id", + ), + ), + migrations.AlterField( + model_name="subregion", + name="country", + field=models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + to="cities_light.country", + to_field="geoname_id", + ), + ), + migrations.AlterField( + model_name="subregion", + name="region", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to="cities_light.region", + to_field="geoname_id", + ), + ), + ]