How to Create a Django Rest Framework-GIS on Ubuntu 16.04 Server (Part 2)

By Grace Amondi, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

This tutorial describes how to create a GIS Django REST framework on an Alibaba Cloud Elastic Compute Server (ECS) instance with Ubuntu 16.04.

In Part 1, we introduction the Django Rest Framework and the initial setup of the Django application environment. Part 2 will involve final steps into bringing the Django REST API to an end. We will be looking at working on creating models, views and populating our database, as well as adding authentication to the REST API. We will also be creating a simple schools listing database system.


You must have completed the steps in Part 1.

Step 1: Create Django Models

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

Before we can create our models let us add the mygeoapi application that we created to our INSTALLED_APPS.

Open geoapi/ and add the following to INSTALLED_APPS:

Open mygeoapi/ by typing the following:

Edit the file with the following:

The school Model(Table) has the columns(attributes) of:

  1. Name — the name of the school
  2. County — county name of school
  3. Enrollment — number of learners
  4. Emmis code — school unique code
  5. Electricity Availability
  6. Exact location — point(x,y)

In GeoJSON each feature can have a properties member containing the attributes of the feature. By default this field is filled with the attributes from your Django model, excluding the id, geometry and bounding box fields. It’s possible to override this behaviour and implement a custom source for the properties member through the use of HStore.

Remeber to add hstore extension to your database;

Next we are going to migrate the tables to our database:

Step 2: Serializing Django Objects

GeoFeatureModelSerializer is a subclass of rest_framework.ModelSerializer which will output data in a format that is GeoJSON compatible.GeoJSON is file format for representing geodata as JSON. Serializing an object list GeoFeatureModelSerializer will create a FeatureCollection.GeoFeatureModelSerializer requires you to define a geo_field to be serialized as the “geometry”.

Create a file in the mygeoapi directory and open using:

Let’s start by importing GeoFeatureModelSerializer, serializers and models we created. Add the following to mygeoapi/ file:

Next let’s create serializer classes for our models.

Save and close file.

Step 3: Creating Views

A view is a callable which takes a request and returns a response.We will be using @api_view wrapper to write API function based views.These wrapper provide a few bits of functionality such as making sure you receive Request instances in your view, and adding context to Response objects so that content negotiation can be performed.

The wrapper also provides behaviour such as returning 405 Method Not Allowed responses when appropriate, and handling any ParseError exception that occurs when accessing with malformed input. Let’s import all dependancies needed and create an api view that will be making get requests.

Open mygeoapi/ by typing the following:

Edit it with the following:

Next we will need to create class based views.Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:

  • Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching.
  • Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.

So let’s add the following to mygeoapi/

You will notice that in the SchoolViewSet’s filter_backends has been assigned DistanceToPointFilter. It returns results within a certain distance from a given point.This means you need to install django filter:

Step 4: Create URLs

Finally we need to wire these views up. Create the mygeoapi/ file and add the following:

We started by importing views we had created and then hooking the urls for each of them.We also need to wire up the root urlconf, in the geoapi/ file, to include our snippet app’s URLs. Your file should appear like this:

Step 5: Make the mygeoapi App Modifiable in the Admin

Just one thing to do: we need to tell the admin that School objects have an admin interface. To do this, open the mygeoapi/ file, and edit it to look like this:

Step 6: Restart the Development Server

Let’s make sure that our app is functioning well. Run the following command to check our progress then open the development server in your browser and you will see a congratulations message.:

In your web browser, visit your server’s domain name or IP address followed by :8000:

Go to http://server_domain_or_IP:8000/admin and you will be welcomed with a new django rest framework admin interface similar to the one shown below.

Image for post
Image for post

Log in using the superuser username and password created earlier.

Once logged in you will see the page below:

Image for post
Image for post

Three api endpoints exist ie

  1. users
  2. groups
  3. schools

Let’s take a look at the schools api endpoint. Use the url http://server_domain_or_IP:8000/schools. You should see something similar to the image below:

Image for post
Image for post

You are allowed to make GET, POST, PUT, and DELETE. The names of the methods are self-explanatory. For example GET enables you to retrieve data from the server. POST enables you to add data to an existing file or resource in the server. PUT lets you replace an existing file or resource in the server. And DELETE lets you delete data from the server.

With that in place you are now able to create and run a Django REST Framework -GIS on your ubuntu 16.04 Alibaba Cloud ECS instance.


We are now able to create a Django Rest Framework that is GIS enabled. You can find and clone the source code on my GitHub.


Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store