django-cities-light icon indicating copy to clipboard operation
django-cities-light copied to clipboard

Region matching query does not exist.

Open jitp opened this issue 4 years ago • 9 comments

I installed the package but when I run cities_light command I get that error when script is importing the cities.

Maybe the issue happens when trying to insert a subregion from a non existing region.

def _get_subregion_id(self, country_code2, region_id, subregion_id):
        """
        Simple lazy identity map for (country_code2, region_id,
        subregion_id)->subregion
        """
        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
        if subregion_id not in self._subregion_codes[country_id]:
            self._subregion_codes[country_id][subregion_id] = \
                SubRegion.objects.get(
                country_id=country_id, geoname_code=subregion_id).pk
        return self._subregion_codes[country_id][subregion_id]

On that function on the first if, a query to Region is made and it raises an exception as it doesn't find it but in the outer function calling the above one there is a try except clause but only catches exceptions from non existing Subregions (second if in the above function).

        try:
            subregion_id = self._get_subregion_id(
                items[ICity.countryCode],
                items[ICity.admin1Code],
                items[ICity.admin2Code]
            )
        except SubRegion.DoesNotExist:
            subregion_id = None

So the Region exception goes freely and breaks everything.

jitp avatar Apr 14 '20 21:04 jitp

Until somebody proposes a fix you can use 3.5.1 which is the same without sub-regions.

jpic avatar Apr 15 '20 08:04 jpic

@jitp WIll be good if you share a stack trace of the error that you received and more information about your stack, like os, python version and the version of Django.

marianoeramirez avatar Apr 17 '20 20:04 marianoeramirez

@jpic on development is fixed this issue and probably another issue.

marianoeramirez avatar Apr 17 '20 23:04 marianoeramirez

Thank you @bomba1990.

I did the same as you did on my local copy of the source code so I could keep going on using it. After that issue another one came up and it's related to some subregions with the same geoname_code. I solved it like this:

       try:
           subregion_id = self._get_subregion_id(
               items[ICity.countryCode],
               items[ICity.admin1Code],
               items[ICity.admin2Code]
           )
       except (SubRegion.DoesNotExist, Region.DoesNotExist):
           subregion_id = None
       except SubRegion.MultipleObjectsReturned as e:
           subregion_id = \
                   SubRegion.objects.filter(
                       country_id=country_id, region_id=region_id, geoname_code=items[ICity.admin2Code])\
                       .all().first().pk
           self._subregion_codes[country_id][subregion_id] = subregion_id

It is a bit out of design but it solved the issue. I think. I'm using python 3.6.9 with django 1.11 on ubuntu 18.04

jitp avatar Apr 18 '20 02:04 jitp

Yes, I found the same issue @jitp. But I see that the issue was on the _get_subregion_id method, the code for the filter by region is doing by country, I change this to filter by region and work perfectly.

marianoeramirez avatar Apr 18 '20 02:04 marianoeramirez

@bomba1990 excellent! I just see your commit with the change in _get_subregion_id.

jitp avatar Apr 18 '20 02:04 jitp

It looks like this issue was never closed and I'm experiencing the same error.

  • Debian GNU/Linux 8.11
  • python 3.6.5
  • Django 2.2.13
  • django-cities-light 3.6.0
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 205, in handle
    self.city_import(items)
  File "/usr/local/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 504, in city_import
    items[ICity.admin2Code]
  File "/usr/local/lib/python3.6/site-packages/cities_light/management/commands/cities_light.py", line 282, in _get_subregion_id
    country_id=country_id, geoname_code=region_id).pk
  File "/usr/local/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 408, in get
    self.model._meta.object_name
cities_light.models.DoesNotExist: Region matching query does not exist.

droptablestar avatar Oct 07 '20 14:10 droptablestar

I am getting the same error any solution? Traceback (most recent call last): File "manage.py", line 30, in execute_from_command_line(sys.argv) File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/init.py", line 381, in execute_from_command_line utility.execute() File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/init.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute output = self.handle(*args, **options) File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/cities_light/management/commands/cities_light.py", line 205, in handle self.city_import(items) File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/cities_light/management/commands/cities_light.py", line 501, in city_import subregion_id = self._get_subregion_id( File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/cities_light/management/commands/cities_light.py", line 281, in _get_subregion_id self._region_codes[country_id][region_id] = Region.objects.get( File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/khalid/Desktop/dukaan/dukandaar-web/dukandaar-web/envv/lib/python3.8/site-packages/django/db/models/query.py", line 406, in get raise self.model.DoesNotExist( cities_light.models.DoesNotExist: Region matching query does not exist.

im-khalidbutt avatar Dec 11 '20 10:12 im-khalidbutt

@im-khalidbutt Please open a new ticket with the error that you have, the versions of Django, and if you can provide more information on your existing database.

marianoeramirez avatar Dec 11 '20 15:12 marianoeramirez