diff --git a/cities_light/management/commands/cities_light.py b/cities_light/management/commands/cities_light.py index 86763112..52268418 100644 --- a/cities_light/management/commands/cities_light.py +++ b/cities_light/management/commands/cities_light.py @@ -313,9 +313,10 @@ def region_import(self, items): except InvalidItems: return + force_insert = False + force_update = False + try: - force_insert = False - force_update = False region = Region.objects.get(geoname_id=items[IRegion.geonameid]) force_update = True except Region.DoesNotExist: @@ -370,17 +371,6 @@ def city_import(self, items): except InvalidItems: return - try: - force_insert = False - force_update = False - city = City.objects.get(geoname_id=items[ICity.geonameid]) - force_update = True - except City.DoesNotExist: - if self.noinsert: - return - city = City(geoname_id=items[ICity.geonameid]) - force_insert = True - try: country_id = self._get_country_id(items[ICity.countryCode]) except Country.DoesNotExist: @@ -397,6 +387,25 @@ def city_import(self, items): except Region.DoesNotExist: region_id = None + force_insert = False + force_update = False + + try: + city = City.objects.get(geoname_id=items[ICity.geonameid]) + force_update = True + except City.DoesNotExist: + try: + # check on duplicate by unique key + city = City.objects.get(name=items[ICity.name], + region_id=region_id) + if city: + return + except City.DoesNotExist: + if self.noinsert: + return + city = City(geoname_id=items[ICity.geonameid]) + force_insert = True + save = False if city.country_id != country_id: city.country_id = country_id