coderholic / django-cities

Countries and cities of the world for Django projects
MIT License
928 stars 372 forks source link

Unspecific except in import command gives misleading output. #160

Closed Antman261 closed 7 years ago

Antman261 commented 7 years ago

Checklist

Steps to reproduce

Import with default values in Django1.10 + GeoDjango + PostGIS.

Expected behavior

Useful exception logging.

Actual behavior

When importing, I was getting a lot of the following errors logged:

Exception ignored in: District: Eden Glen Ext 60: DB backend does not support native '.distance(...)' query falling back to two degree search

When I checked the code throwing this error I found this:

                try:
                    if django_version < (1, 9):
                        city = City.objects.filter(population__gt=city_pop_min)\
                                   .distance(defaults['location'])\
                                   .order_by('distance')[0]
                    else:
                        city = City.objects.filter(population__gt=city_pop_min)\
                            .annotate(distance=Distance('location', defaults['location']))\
                            .order_by('distance')[0]
                except:  # TODO: Restrict what this catches
                    self.logger.warning(
                        "District: %s: DB backend does not support native '.distance(...)' query "
                        "falling back to two degree search",
                        defaults['name']
                    )

I checked that I could use Distance with the following:

>>> from django.contrib.gis.db.models.functions import Distance
>>> from main.models import Restaurant
>>> from django.contrib.gis.measure import D
>>> rest1 = Restaurant.objects.get(slug='harvest-espresso')
>>> rests = Restaurant.objects.filter(location__distance_lte=(rest1.location, D(km=1000))).annotate(distance=Distance('location', rest1.location)).order_by('distance')
>>> rests
<QuerySet [<Restaurant: Harvest Espresso>, <Restaurant: Pachi Pachi>, <Restaurant: Nobu>, <Restaurant: Hey Griller>, <Restaurant: Hey Griller>,....
blag commented 7 years ago

That's simply a warning that - while annoying - you can safely ignore. If the log spam that code generates annoys you enough you can adjust your logging level by settings the cities logger to above WARNING (eg: ERROR or CRITICAL). See the Python logging cookbook for more on logging.

I am working on fixing this. See #161 for details and progress.

blag commented 7 years ago

Can you check my new-stuff branch to see if it generates that much log spam?

I fixed the database query in ba2150ce86e4b0a019114748ad39d990a3f1e51f and restricted the exceptions we ignore in 1e6dffb2231183833b4e0bb51e732264a6aa5b16.

Thanks!

blag commented 7 years ago

I just merged #161 and released version 0.5.0.4 to PyPI which should fix these issues.

Closing. Please upgrade and reopen this issue if it isn't fixed. Thanks!