API Development with Django and Django Rest Framework

INTERSECT helps businesses create and deliver exceptional digital products that marry business objectives and user needs. Learn more.

Python & Django

Django is a powerful framework written in Python that offers a variety of development tools. Its slogan: “The Web framework for perfectionists with deadlines” succinctly articulates its spirit. By providing high-level development libraries, Django effectively liberates developers from much of the tediousness and minutia of application development such as validation, database modeling, authentication, etc.

In this developer’s opinion, the ORM (Object-relational mapping) is perhaps its greatest feature. It provides a simple object-oriented representation of data which is translated into relational database schemas. A developer can create an entire database schema by simply creating model classes. Interaction with the database is possible with very little code. Gone are the days of long SQL statements to perform simple select, insert,““ update or delete statements. Furthermore, by using Django’s ORM, database backends can easily be swapped out with little or no modification of the application code (ie. switching from MySQL to Postgres)

Django also provides us with a powerful forms system that performs validation and CRUD operations on a given model and will even generate a fully functional HTML form. Forms are extremely flexible and easily customizable.

Decoupling the Backend

API development allows us to separate our backend core from our user interfaces. This allows the development of a central application core that can be consumed simultaneously by a Web interface, native mobile applications or software for the desktop.

In the past, our REST APIs consisted of ‘pages’ that would simply return JSON strings and would accept and parse post data. Building an API that performed CRUD (create, retrieve, update and delete) operations had to be explicitly coded to do so. Custom code needed to be written to handle and process the allowable HTTP requests types and return the appropriate response.

Django REST Framework

The Django REST Framework is a powerful open-source tool used to build REST APIs and is the preferred development library at BNOTIONS. Its advantages include its simplicity, flexibility, and quality source code and documentation. The version 2 features a powerful serialization engine that behaves much like Django forms, token based authentication (integration with OAuth and OAuth2 protocols), generic classes for CRUD operations, throttling, permission classes, etc. The framework provides developers with the flexibility to extend and customize the tools offered as they see fit, and greatly reduces development time.

The example below demonstrates how quickly one can build a basic API endpoint with just a few lines of code.

An example Django model class (database table):

The Serializer (converts query data into native Python datatypes)

The view (handles HTTP Requests/Responses)

In the example above, the API is directly interacting with the Cigar model to list and create new records in the database in roughly 6 lines of code.

The Django REST Framework provides us a useful and attractive user interface to interact with our API endpoints during development which can easily be turned off in production.

Django REST Framework Docs

The Django REST Framework provides us with a browsable API and provides some auto-generated documentation in its user interface. For example, the page title is extracted from the URL pattern’s name attribute and a description has been extracted from the API view’s doctstring.

Keeping documentation updated is a constant challenge during development, particularly when changes to our API directly affect other developers. We want our web and mobile teams to have access to an accurate reference to the APIs and their functionality. We also want to free the API developers from having to constantly update a text document each time they make a change to the codebase.

Django REST Framework Docs was created as an attempt to solve this problem in our projects at BNOTIONS. The tool analyzes the code and produces an inventory of all the Django REST Framework endpoints in a project. The tool examines comments in the code, the fields that are exposed, validation constraints, acceptable methods, etc. for each endpoint. This is done in real-time, meaning that a change to the code will immediately be reflected in the documentation.

Final thoughts

Since switching to Python & Django almost a year ago, we have significantly improved our development process. Using tools like Django REST Framework has allowed us to approach problems from a higher level and concentrate on strategy & functionality rather than implementation.

By using open source development software, our developers are also active contributors to many projects, helping improve tools that have in turn helped us become better developers.

Django Framework (http://www.djangoproject.com/)
Django REST Framework (http://django-rest-framework.org/)
Django REST Framework Docs (https://github.com/marcgibbons/django-rest-framework-docs)

Marc Gibbons
Software Engineer – Python