CRUD is an approach to build a Django web application. CRUD stands for Create Retrieve Update Delete.

In simple words CRUD means performing Create, Retrieve, Update and Delete operations on a table in a database.

Django REST provides some generic view classes for doing these operations easily and there is an HTTP method handler assigned with each of them.

CRUDHTTP VerbUsage
CreatePOST(Creates a new instance of a model)
RetrieveGET(Brings the value of an instance)
UpdatePUT/PATCH(Updates the value of an instance)
DeleteDELETE(Deletes an instance)

In this article we will implement each of the operations in our project. First, we will create one API for each operation. Then we will see how to do multiple operations with only one API endpoint.

Firstly, we will have to import generic API views from rest_framework and serializers from our serializers.py file.

# CRUD OPERATION
from rest_framework.generics import ( 
    CreateAPIView,
    RetrieveAPIView, 
    UpdateAPIView, 
    DestroyAPIView, 
)
from .serializers import PersonSerializers 

CreateAPIView

When we need to create a new object of a model, we use this class to create an create-only endpoint. It works like a form. This is the concrete view for creating a model instance. It provides a post method handler.

class PersonCreateAPIView(CreateAPIView):
    serializer_class = PersonSerializers

CreateAPIView inherits properties from CreateModelMixin, GenericAPIView. It has many more attributes and methods.

API Endpoint (URL)

urlpatterns = [

path('api/create-person/',PersonCreateAPIView.as_view()),

]

Output

RetrieveAPIView

Is used for read-only endpoints. It represents a single model instance and provides a get method handler. It inherits properties of GenericAPIView & RetrieveModelMixin.

class PersonDetailAPIView(RetrieveAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializers

Endpoint

urlpatterns = [

path('api/person-detail/<int:pk>',PersonDetailAPIView.as_view()),

]

Output

UpdateAPIView

Used for updating only a single model instance. It provides put and patch method handlers. PUT is a method of updating object where the client sends data that updates the entire object. Unlike PUT, PATCH applies a partial update to the object.

class PersonUpdateAPIView(UpdateAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializers

Endpoint

urlpatterns = [

  path('api/person-update/<int:pk>',PersonUpdateAPIView.as_view()),

]

Output

DestroyAPIView

It is used for delete-only endpoints for a single model instance. Provides a delete method handler.

class PersonDeleteAPIView(DestroyAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializers

Endpoint

urlpatterns = [

path('api/person-delete/<int:pk>',PersonDeleteAPIView.as_view()),

]

Output

If we hit the endpoint and press the ‘RED DELETE’ button django will show a confirmation box before deleting an object.

Doing Multiple operations with one API endpoint

We have seen each of CRUD operations specifically. Some times we need to do more than one operation with an endpoint. Then writing multiple views and repeating same lines of code violates DRY principle. Django REST offers a solution of this problem. There are some views which can handle multiple operations within a single API view.

    RetrieveDestroyAPIView – GET & DELETE

    RetrieveUpdateAPIView – GET, PUT & PATCH

    RetrieveUpdateDestroyAPIView – GET, PUT, PATCH & DELETE

Views

from rest_framework.generics import ( 

    RetrieveDestroyAPIView,
    RetrieveUpdateAPIView,
    RetrieveUpdateDestroyAPIView,
)

from .serializers import PersonSerializers 


class PersonRetriveUpdateAPIView(RetrieveUpdateAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializers


class PersonRetriveDeleteAPIView(RetrieveDestroyAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializers


class PersonRetrieveUpdateDestroyAPIView(RetrieveUpdateDestroyAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializers

Endpoints for these three views

urlpatterns = [

path('api/person-update-delete/<int:pk>',PersonRetriveUpdateAPIView.as_view()),
path('api/person-get-delete/<int:pk>',PersonRetriveDeleteAPIView.as_view()),
path('api/person-get-update-delete/<int:pk>',PersonRetrieveUpdateDestroyAPIView.as_view()),

]

These generic classes save time and make code DRY. Now add these codes in your project and see how the output looks. If you want you can see here the source code of these generic view classes.

+ posts

Author | Python-Django Developer

+ posts

Full-stack Developer (Python | Django | React | React-Native | Angular | Vue)