Add Location Point to Django, PostgreSQL app

Saeed Babashahi
3 min readNov 29, 2020

In this article we will create a simple Django app with the support of location point and showing location point on Django admin map. I assume readers of this article are familiar with Django and PostgreSQL setup so I just write about new parts that need to be handled.

This is my first article on medium, I will be glad to have your feed-backs :)

Photo by Sylwia Bartyzel on Unsplash

Spatial Database is a database that supports storing and utilizing information about location. Location can be Point, Line and Polygon. In this article we just add support of point location to our app. By default PostgreSQL does not support this feature and we need to install a new extension called “postgis”. Before installing the extension, it is necessary to install libraries bellow. They are mandatory for using postgis.

sudo apt-get install binutils libproj-dev gdal-bin

Above command is for Ubuntu, For Mac and Windows and more information about Geo libraries go to Django docs. Now we need to install the postgis extension to our PostgreSQL. Go to your PostgreSQL Database and setup postgis in it. <db name> is your database name that is connected to your app.

sudo su postgres
psql <db name>
CREATE EXTENSION postgis;

For simplicity I declare my model with just one field as bellow.

from django.contrib.gis.db import modelsclass Location(models.Model):
location_point = models.PointField(null=True, blank=True)
# other fields you need...

After that add your app to INSTALLED_APPS settings.py, makemigration and migrate. Add this code to your app admin.py.

from django.contrib import admin
from location.models import Location # Suppose location is the name of app :)
@admin.register(Location)
class LocationAdmin(admin.ModelAdmin):
fields = ( 'location_point', )

Let’s check how Django admin of our app looks like.

Default Django admin map view

This is the default Django admin map view. But I think, it’s not so useful because it doesn’t show cities and streets. To add support of more detailed maps we must use Django apps. From apps that can help us, I choose django-location-field because of simplicity of utilization. First we need to install it in our project.

pip install django-location-field

After adding location_field.apps.DefaultConfig to your INSTALLED_APPS, add bellow configs to setting.py. This app supports utilizing Google map and Open street map. Since open street map is free I prefer to use it.

LOCATION_FIELD = {
‘map.provider’: ‘openstreetmap’,
‘map.zoom’: 15,
‘search.provider’: ‘google’,
‘search.suffix’: ‘’,
# # Google
# ‘provider.google.api’: ‘//maps.google.com/maps/api/js?sensor=false’,
# ‘provider.google.api_key’: ‘<INSERT GOOGLE API KEY>’,
# ‘provider.google.api_libraries’: ‘’,
# ‘provider.google.map.type’: ‘ROADMAP’,
# OpenStreetMap
‘provider.openstreetmap.max_zoom’: 16,
}

Now we need to change our model with the code bellow.

from location_field.models.spatial import LocationFieldclass Location(models.Model):
location_point = LocationField(based_fields=['city'], zoom=7, default=Point(51.67, 32.65), null=True, blank=True)

It’s necessary to note something on “default Point” from the above code. This class is initialed by x and y axis but in real geometry Latitude and longitude are reverse of this. So my default point in geometry is 32.65, 51.67.

There is another field called PlainLocationField that can work the same as LocationField but the difference is that PlainLocationField saves the latitude and longitude as Plaint string in database which I don’t prefer but I mentioned it for those who don’t like to use postgis. If we check Django admin again it would be something like this.

Feel free to comment any problems and issues with this article, I will be glad to learn from you.

--

--

Saeed Babashahi

Backend software engineer who loves to learn. I am experienced in Python, Django, DRF, Neo4j, PostgreSQL and I am a newbie gopher :).