In the world of web development and APIs, the DELETE method serves a vital purpose: it allows clients to request the removal of a specific resource from the server. In Django Rest Framework (DRF), the DELETE method is employed to delete resources on the server, providing a robust way to manage your data. In this blog, we will dive into the DELETE method in DRF and showcase its implementation with a practical example.
Read our New Blog on How to Combine all API Methods like Get, Post, Put, Delete in One View
The DELETE Method Request in Django Rest Framework
The DELETE method is used to request the removal of a resource identified by a unique identifier, typically the primary key (id
). When a client sends a DELETE request to the server, it targets a specific resource, and the server proceeds to delete that resource parmanently. The DELETE method is considered an idempotent operation, meaning that multiple identical DELETE requests will result in the same outcome without side effects.
DRF simplifies the handling of DELETE requests by providing built-in functionalities to process and execute resource deletions, ensuring data consistency and security.
Prerequisites:
Before we begin, ensure you have the following prerequisites in place:
- Django installed on your system.
- Basic familiarity with Django project structure and database concepts.
- Virtual Environment, this is optional but recommended. You check our blog here.
Note: For this tutorial, we are using our basic skeleton project for Django. You can also download the project from here.
Sample Django Model and Serializers for Examples
Step 1: Create the Django Model
First, define the Django model that represents a book. In your app’s models.py
, add the following code:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
publication_date = models.DateField()
Don’t forget to run python manage.py makemigrations
and python manage.py migrate
to create the corresponding database table for the model.
Step 2: Create the Serializer
Next, create a serializer to convert the book model instances into JSON data and vice versa. In your app’s serializers.py
, add the following code:
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title', 'author', 'publication_date']
1. Building a Django Rest Framework API with the DELETE Method
Let’s create a simple API to manage a collection of books and demonstrate how the DELETE method functions. Before proceeding, ensure that Django and Django Rest Framework are installed in your project.
Step 1: Create the View
Now, create a view to handle DELETE requests for removing existing books. In your app’s views.py
, use the DestroyAPIView
provided by DRF:
from rest_framework import generics
from .models import Book
from .serializers import BookSerializer
class BookDeleteView(generics.DestroyAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
Step 2: Define the URL Pattern
Define a URL pattern to map the view with a URL endpoint that includes the unique identifier (id
) of the book to be deleted. In your project’s urls.py
, include the following code:
from django.urls import path
from .views import BookDeleteView
urlpatterns = [
path('api/books/<int:pk>/', BookDeleteView.as_view(), name='book-delete'),
]
Step 3: Test the API with the DELETE Method
With the API set up, test the DELETE method to remove an existing book. Utilize tools like curl
or Postman to send a DELETE request to the URL http://localhost:8000/api/books/<book_id>/
, where <book_id>
is the unique identifier (primary key) of the book to be deleted.
For example, using curl
, delete the book with id=1
with the following command:
curl -X DELETE http://localhost:8000/api/books/1/
The server will process the request and remove the specified book permanently.
Conclusion
In this blog post, we explored the DELETE method in Django Rest Framework, which is used to delete specific resources in the API. We walked through a step-by-step example of creating a simple API to manage books, covering model creation, serializer definition, view creation, and URL configuration. DRF’s builiin features make it easy to handle data deletions, ensuring that resources can be removed securely and efficiently from your web application.
Find this tutorial on Github.