Joseph Kasule
5 min readNov 12, 2018

Django Rest Framework, A Beginner’s guide

Django REST framework is a powerful, scalable, and versatile toolkit for constructing web APIs. I recommend someone taking this tutorial to have the basic knowledge of Django and REST APIs. If you haven’t done any Django before please refer to this documentation Django for reference.

Let’s get started...

We are going to create a simple API for the fast food fast app which I created previously in flask restful. you can get the flask restful version from my repo.

Project setup.

Create a new Django project called fast-foods and start an application called fast_foods_api,

#create a project directory
mkdir fast-food-fast
cd fast-food-fast
#Create a new virtualenv
virtualenv env
source env/bin/activate # On Windows use `env\Scripts\activate`
#Install Django and Django REST framework into the virtualenv
pip install django
pip install djangorestframework
#Start up a new project
django-admin startproject fast_foods
cd fast_foods
#start up an application with in the project
django-admin startapp fast_foods_api

After successful installation, you should have a project file structure similar to mine below.

app file structure

Now that we have the project created let us migrate the existing models these are basically user models for now. To do so run python manage.py migrate. Let us start the server now, run python manage.py runsever. You should get a congratulation message when you run the localhost URL in the browser.

Alright, it’s getting more fun.. Let us add the relevant apps we installed to the settings, to do this navigate to fast_foods/settings.py and add rest_framework and fast_foods_api in your settings. The INSTALLED_APPS in settings should now look like these.

setting.py

Models

So to start up with our API the first thing we need to do is to create models because the API will be requesting models from the database as well as performing other CRUD functionalities. let’s create a model, navigate to fast_foods/fast_foods_api/models.py. let’s then create a new single model class called Meal which will inherit from models.Model.

fast_food_api/models.py

Every time you make changes to a model file remember to make migrations to sync the database.

#make migrations and migrate changes
python manage.py makemigrations
python manage.py migrate

Serializers

Now that we have the models working fine, the next thing we need to do is to add serializers. What serializers do in the context of an API is to translate to and from JSON or technically it can be any content format that can be sent over HTTP like XML. The meal model will just exist on our laptops but we can not send it over HTTP so we need a common data format for our data representation and that is the purpose of serializers. They are responsible for turning our models into RESOURCES in our API when connected to a ViewSet.

Having understood clearly what serializers do, lets now create a serializer.py file in fast_foods_api. add the following lines of code below.

fast_foods_api/serializer.py

Views

We are done with serializers lets now move on to the views, navigate to fast_foods_api/views.py. We need a view that handles the logic of combining model, serializer, and eventually URLs together. let us create a new MealView class which inherits a ModelViewSet class from a viewsets model. The viewset.ModelViewSet simplifies for our work of not defining every request method explicitly but rather we just inherit a class that has all method predefined and our task will now be getting the data from the database.

fast_foods_api/views.py

Urls

Almost done, Now the final step is to configure our URLs in the topmost, project level. open fast_foods/urls.py, import the required packages and files.

from django.conf.urls import url, include
from django.contrib import admin
from fast_foods_api import views
from rest_framework import routers
#router take care of generating automatically all urls for our model
router = routers.DefaultRouter()
#register the views we have, 1st argument takes the endpoint, 2rd #argument is the actual view for that endpoint.
router.register('', views.MealView)
#pass the urls for our endpoints
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(router.urls)),
]
urls.py

Yap. seems everything is moving kawa, let us run the server again with python manage.py runserver and browse to your localhost directory. it’s looks awesome right? cool. hope you have something similar to mine.

you can try to perform other request methods like updating, deleting and getting a food item. let me try to update an item at id 1 on URL http://127.0.0.1:8000/meal/1/..

CONCLUSION

Okay, that’s it for this part 1, I will keep you posted with part 2 were we will explore more Django rest framework.

REFERENCE RESOURCES

Joseph Kasule

Full-stack Engineer (Python, Javascript) | QA Engineer | Mentor | Entrepreneur