How to make/use a custom database function in Django

Django provides the Func() expression to facilitate the calling of database functions in a queryset: Func() expressions are the base type of all expressions that involve database functions like COALESCE and LOWER, or aggregates like SUM. There are 2 options on how to use a database function in Django/GeoDjango ORM: For convenience, let us assume … Read more

Getting ‘DatabaseOperations’ object has no attribute ‘geo_db_type’ error when doing a syncdb

The OP was using the GeoDjango buildpack, but in case anyone gets here using Geo buildpack and dj_database_url like I was, in settings.py don’t forget the last line: import dj_database_url DATABASES[‘default’] = dj_database_url.config() DATABASES[‘default’][‘ENGINE’] = ‘django.contrib.gis.db.backends.postgis’ UPDATE dj_database_url directly supports PostGIS. You can do without the last line in the code above if you can … Read more

GeoDjango on Windows: “Could not find the GDAL library” / “OSError: [WinError 126] The specified module could not be found”

I have found the following to work for windows: Run python to check if your python is 32 or 64 bit. Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or C:\OSGeo4W64: Note: Select Express Web-GIS Install and click next. In the ‘Select Packages’ list, ensure that GDAL is selected; MapServer and Apache are also … Read more

Django sort by distance

the .distance(ref_location) is removed in django >=1.9 you should use an annotation instead. from django.contrib.gis.db.models.functions import Distance from django.contrib.gis.measure import D from django.contrib.gis.geos import Point ref_location = Point(1.232433, 1.2323232, srid=4326) yourmodel.objects.filter(location__distance_lte=(ref_location, D(m=2000))) .annotate(distance=Distance(“location”, ref_location)) .order_by(“distance”) also you should narrow down your search with the dwithin operator which uses the spatial index, distance does not use … Read more