Django Rest Framework Basic CRUD

REST is the underlying architectural principle of the web. The amazing thing about the web is the fact that clients and servers can interact in complex ways without the client knowing anything beforehand about the server and the resources it hosts. An API that adheres to the principles of REST does not require the client to know anything about the structure of the API. Rather, the server needs to provide whatever information the client needs to interact with the service.

Django REST framework is a powerful and flexible toolkit for building Web APIs. In this article we going to see basic CRUD ( Create Read Update Delete ) operation for a Contact Table. Here we will test the API url's using Postman tool.

Installation

$ pip install djangorestframework

Add 'rest_framework' to your INSTALLED_APPS in setting.py

INSTALLED_APPS = [
    ...
    'rest_framework',
]

If you're intending to use the browsable API, add the following to your root urls.py

urlpatterns = [
    ...
    url(r'^api-auth/', include('rest_framework.urls'))
]

models.py

from django.db import models

class Contact(models.Model):
    name = models.CharField(max_length=128)
    email = models.CharField(max_length=128)
    phone = models.CharField(max_length=128)

    def __str__(self):
        return self.name

Add some initial data to the table via django admin

serializers.py

from rest_framework import serializers

from models import Contact

class ContactSerializer(serializers.ModelSerializer):
    class Meta:
        model = Contact
        fields = "__all__"

Contact List:

urls.py

url(r'api/contact/list/$', ContacttList.as_view())

views.py

from rest_framework.generics import ListAPIView

from models import Contact
from serializers import ContactSerializer

class ContacttList(ListAPIView):
    queryset = Contact.objects.all().order_by('-id')
    serializer_class = ContactSerializer

HTTP Method : GET

Add Contact:

urls.py

url(r'api/contact/add/$', AddContact.as_view())

views.py

from rest_framework.generics import CreateAPIView

from models import Contact
from serializers import ContactSerializer

class AddContact(CreateAPIView):
    queryset = Contact.objects.all().order_by('-id')
    serializer_class = ContactSerializer

HTTP Method : POST

Retrieve Contact:

urls.py

url(r'api/contact/(?P<pk>[0-9]+)/$', RetrieveContact.as_view())

views.py

from rest_framework.generics import RetrieveAPIView

from models import Contact
from serializers import ContactSerializer

class RetrieveContact(RetrieveAPIView):
    queryset = Contact.objects.all().order_by('-id')
    serializer_class = ContactSerializer

HTTP Method : GET

Edit Contact:

urls.py

url(r'api/contact/(?P<pk>[0-9]+)/edit/$', EditContact.as_view())

views.py

from rest_framework.generics import UpdateAPIView

from models import Contact
from serializers import ContactSerializer

class EditContact(UpdateAPIView):
    queryset = Contact.objects.all().order_by('-id')
    serializer_class = ContactSerializer

HTTP Method : PUT

Delete Contact:

urls.py

url(r'api/contact/(?P<pk>[0-9]+)/delete/$', DeleteContact.as_view()),

views.py

from rest_framework.generics import DestroyAPIView

from models import Contact
from serializers import ContactSerializer

class DeleteContact(DestroyAPIView):
    queryset = Contact.objects.all().order_by('-id')
    serializer_class = ContactSerializer

HTTP Method : DELETE

That's it and now we can enjoy the Restfull API's with Django and I hope you enjoyed this article, if you face any issues or problem, please feel free to give your feedbacks in the comment secton. You will find the source code here.

  • Django
  • Rest Framework
  • CRUD