Back
Feb 28, 2017

How to write an API in Django

Vladimir Kalyuzhny

Introduction

There is such a term as Remote Procedure Call (RPC). In other words, by using this technology, programs can call functions on remote computers. There are many ways to implement RPC. Here are some of them related to web technologies and this is what we will focus on:

Each of these technologies can be used in Django. In this article we will look at the libraries which add RPC support for Django.

REST

Let's start with the most common protocol - REST. REST stands for Representational State Transfer - transfer of the state representation. In fact, REST is an architectural style, not the protocol. For example, SOAP is a protocol. Due to its popularity and prevalence there is a large selection of libraries for Django which allow you to turn your views into REST views by making simple actions.

In the world of Django REST API there are a few of the most popular libraries, and we will compare them. In comparison table I have identified the most important factors, in my opinion, that are relevant when choosing the library.

 Django REST frameworkTastypiePistonDjango XML-RPCDjango REST Pandas
Version3.5.3 (Production/Stable)0.13.3 (Beta)0.2.3 (Alpha)0.1.7 (Production/Stable)0.5.0 (Production/Stable)
Python 3 supportYesYesNoYes0Yes
Repositorydjango-rest-frameworkdjango-tastypiedjango-pistondjango-xmlrpcdjango-rest-pandas
Stars7107312896248379
Forks243210813041531
Documentationdjango-rest-framework.orgdjango-tastypie.readthedocs.io/en/latest/bitbucket.org/jespern/django-piston/wiki/Documentationgithub.com/Fantomas42/django-xmlrpcgithub.com/wq/django-rest-pandas
API key authenticationYesYesNo--
OAuthYesYesNo--
OAuth2YesYesNo--
Accept HeadersYesYesNo-via DRF
SerializationsJSON, JSONp, XML, YAML, HTML, MessagePack, CSVJSON, JSONp, XML, YAML, HTML, plistXML, JSON, YAML, Pickle, Django-CSV, TXT, XLS, XLSX, JSON, PNG, SVG

So, we can summarize, if your API architecture is complex (mobile devices support, versioning, Mongo models serialization), it is better to choose Django REST framework. Tastypie and Piston can be used for small, simple projects.

SOAP

Nowadays, REST is the most popular. But there may be situations when you need to have an interface for legacy products. The library, which adds SOAP support in django, is called spyne. Let's see how this works in django. First, install the pip install spyne package and add 'rpctest.core' into INSTALLED_APPS.

views.py

false

urls.py

false

Let's make a curl request to check our test application:

false

As a result, the output we get:

false

As you can see, there is nothing complicated, spyne library does all the work. Spyne has a lot of settings, for example, if it is necessary to get JSON instead of XML as a response from the API, it is done as follows:

false

Now the response from the API will be rendered in JSON

false
false

Apart from Django, spyne.io supports Twisted and Pyramid.

Protocol Buffers

Protocol Buffers is a new serialization protocol, which was proposed by Google as XML replacement. As Google claims, buffers protocol is easier, faster and smaller than XML. Officially, there is support for C ++, C #, Go, Java and Python. Other languages are supported by third-party developers.

The first thing you need is to create .proto file, where the structure will be described.

false

In our example, the Person object is described: it has two mandatory fields - id/name, one optional field - email, and PhoneNumber field, which can contain user’s multiple phone numbers.

When .proto is created, it should be compiled. This is done with help of protobuf library.

false

After compiling person_pb2.py file will be created.

false

Now imagine that we have a view, which shows information about the user. As an example, I will use the simplest way to show how Protocol Buffer can work in Django.

false

GraphQL

In 2015, Facebook introduced a new standard in the public declaration of data structures and methods of obtaining data - GraphQL. Among the main features of GraphQL we can point out the next ones:

  • strong typing
  • interface support
  • documented representation of the circuit in the form of data structures
  • independence from the version of the server API

For more information about GraphQL you can read the articles of my colleagues in our blog How to Use GraphQL with Django and How to Use GraphQL with Angular 2.

Conclusion

As you can see from the article, among all the technologies for API implementation in Django, REST leads by the number of libraries. The popularity of REST is characterized by its simplicity and functionality. It is also important to note that the data is transmitted without the use of additional layers, so REST is considered to be less resource-intensive, unlike SOAP or XML-RPC. But it also has disadvantages, like any other technology. When you select a backend for the API, be guided by your needs rather than popularity.

More thoughts