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 193225ca..efc73503 100644 --- a/src/cities_light/management/commands/cities_light.py +++ b/src/cities_light/management/commands/cities_light.py @@ -273,7 +273,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] @@ -284,7 +284,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] @@ -296,13 +296,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): 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", + ), + ), + ]