Introduction to API development with Django REST framework

Reading Time : ~ .

Django REST framework is  a best toolkit to create an API
It supports both ORM and Non-ORM data sources.
It can support regular function based view and class based views.

1) Installation of Django REST framework.

pip install djangorestframework

2) Add 'rest_framework'  in 'INSTALLED_APPS'

settings.py

INSTALLED_APPS = (
    ...
    'rest_framework',
    ...
)

3) configure the Django REST framework with 'REST_FRAMEWORK

This framework already contains some default configurations though we can override them like below

settings.py


REST_FRAMEWORK = {
    ....
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    ....
}

Now, we are ready to use the Django REST framework. It is very similar to the Django.

Now create your app and add it to INSTALLED_APPS in settings.py. consider the below code for example. we are writing both function and class based views. You can use function based or class based based on your comfort.

models.py

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
GENDER_CHOICES = (
    ('Male', 'Male'),
    ('Female', 'Female'),
    ('Other', 'Other')
)

class User(AbstractBaseUser, PermissionsMixin):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=100, blank=True, null=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    dob = models.DateField(null=True)
    phone = models.CharField(max_length=20, null=True)
    gender = models.CharField(choices=GENDER_CHOICES, max_length=6)
    address = models.TextField()
    password = models.CharField(maxlength=255)

    def __str__(self):
        return self.email

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^user/create/$', views.UserCreateView.as_view(), name="user_create"),
    url(r'^users/list/$', views.UsersListView.as_view(), name="users_list"),
    url(r'^users/(?P<pk>\d+)/detail/$', views.UserDetailView.as_view(), name="user_detail"),
    url(r'^users/(?P<pk>\d+)/update/$', views.UserUpdateView.as_view(), name="user_update"),
    url(r'^users/(?P<pk>\d+)/delete/$', views.UserDeleteView.as_view(), name="user_delete"),    
]

1. User Create View

serializer for validation( similar to django forms)

serializers.py

from rest_framework import serializers
from .models import GENDER_CHOICES, User

# normal serializer [similar to forms.Form]
class UserSerializer(serializers.Serializer):
    first_name = serializers.CharField(max_length=100)
    last_name = serializers.CharField(max_length=100)
    email = serializers.EmailField()
    username = serializers.CharField(max_length=100)
    dob = serializers.DateField()
    phone = serializers.CharField(max_length=20)
    gender = serializers.ChoiceField(choices=GENDER_CHOICES)
    address = serializers.TextField()
    password = serializers.CharField(max_length=255) 
    # is called if we save serializer if it do not have an instance
    def create(self, validated_data):
       password = validated_data.pop("password")
       user = User.objects.create(**validated_data)
       if password:
           user.set_password(password)
           user.save()
       return user
    # is called if we save serializer if it have an instance
    def update(self, instance, validated_data):
       password = validated_data.pop("password")
       instance.__dict__.update(validated_data)
       if password:
           instance.set_password(password)
       instance.save()
       return instance

# model serializer [similar to forms.ModelForm]
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ("first_name", "last_name", "email", "username", "dob", "phone", "gender", "address")

generic_views.py
from rest_framework import generics
from .serilizers import  UserSerializer

class UserCreateView(generics.CreateAPIView):
    serializer_class = UserSerializer

function_views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serilizers import UserSerializer


@api_view(["POST"])
def create_user(request):
    serializer = UserSerializer(request.data)
    if serializer.is_valid():
        serializer.save()
        return Response({"message": "User created"}) 
    else:
        data = {
          "error": True,
          "errors": serializer.errors,          
        }
        return Response(data)        

2. User Detail View

generic_views.py


from rest_framework import generics
from .serilizers import  UserSerializer

class UserDetailView(generics.RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

function_views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serilizers import UserSerializer
from .models import *

@api_view(["GET"])
def user_details(request, pk):
    user = User.objects.get(id=pk)
    serializer = UserSerializer(user)
    return Response(serializer.data)

3. User Update View 

generic_views.py

from rest_framework import generics
from .serilizers import  UserSerializer

class UserUpdateView(generics.RetrieveUpdateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

function_views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serilizers import UserSerializer
from .models import *

@api_view(["GET", "PUT"])
def user_update(request, pk):
    user = User.objects.get(id=pk)
    if request.method == "PUT":
        serializer = UserSerializer(user, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response({"error": serializer.errors, "error": True}) 
    serializer = UserSerializer(user)
    return Response(serializer.data)


3. User List View 

generic_views.py


from rest_framework import generics
from .serilizers import  UserSerializer

class UsersListView(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

function_views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .serilizers import UserSerializer
from .models import *

@api_view(["GET"])
def users_list(request):
    users = User.objects.all()
    serializer = UserSerializer(users, many=True)
    return Response(serializer.data)
4. User DeleteView

generic_views.py

from rest_framework import mixins
from.models import *

class UserDeleteView(mixins.RetrieveModelMixin, mixins.DestroyModelMixin):
    queryset = User.objects.all()
    
    def get(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

function_views.py

from .models import *
from rest_framework.decorators import api_view
from rest_framework.response import Response

def delete_user(request, pk):
    user = get_object_or_404(User, id=pk)
    user.delete()
    return Response({"message": "Deleted"})
    
for more implementation details please visit http://www.django-rest-framework.org/#tutorial
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Python Memcached Implementation for Django project Ashwin Kumar

Memcache is a memory caching system that helps web applications and mobile app backends to improve performance and scalability. We should consider using Memcache when ...

Continue Reading...
How to customize Django Oscar Models, Views and URL's Chaitanya Kattineni

In order to customize models, views and urls, you need to fork an Oscar core app in which model/view resides. Then you should be able ...

Continue Reading...
Integrate Django-Oscar-Accounts with Django-Oscar Shirisha Gaddi

This package uses double-entry bookkeeping where every transaction is recorded twice (once for the source and once for the destination). This ensures the books always ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties