Django REST framework (DRF) is a powerful and flexible toolkit for building web APIs. In this tutorial, we’ll learn how to build a CRUD API in just 15 minutes using the Django REST framework Delete is a part of it.

To build our sample to-do list application, we’ll start by setting up the Django REST framework in a Django project, followed by a complete tutorial on how to create a CRUD REST API with the Django REST framework.

Use my previous article to know about Get , Post , Put Methods

Use Previous project or Create a Django project called drinks with the following command

django-admin startproject drinks

Then, cd into the new drinks folder and create a new app for your API

python startapp core

Run your initial migrations of the built-in user model:

python migrate

Next, add rest_framework and drinks to the INSTALLED_APPS inside the file:


Create a model in file like

from django.db import models

# Create your models here.

class Drink(models.Model):
  description = models.CharField(max_length=500)

  def __str__(self):
    return" "+self.description

Then apply Commands and Create a SuperUser to Our project:

python makemigrations
python migrate
python createsuperuser

Model serializer

To convert the Model object to an API-appropriate format like JSON, Django REST framework uses the ModelSerializer class to convert any model to serialized JSON objects:

from rest_framework import serializers
from .models import Drink

class DrinkSerializer(serializers.ModelSerializer):
  class Meta:
    model= Drink
    fields = '__all__'

Delete Method

The DELETE() method fetches the to-do object if is available in the database, deletes it, and responds with a response.

Deleting Entities – The DELETE Request Handler

We typically add, add, add and then remember we want to buy some items in multiples, as gifts – until the billing section arrives. The reality check typically makes us remove a few things from the cart that we don’t really need, and reasses our logic.

A user needs to be able to remove certain items from a cart – if they add it by accident, or simply change their mind.

To remove an item from the cart, let’s implement the delete() function, passing in the id of the object we’d like to delete. Then, calling delete() on the model itself, we can remove it from persistence.

Creating API Views In Django

class drink_detail(APIView):

   def delete(self, request, id=None):
        drinks = Drink.objects.filter(id=id)
        return Response(status=status.HTTP_204_NO_CONTENT)

Let’s try to remove the item from our cart:

Run the Django server:

python runserver

Now, we’re ready for the first test. Navigate to Make sure you’re logged in with your superuser credentials

Since we need to pass in an actual instance, we’ll have to use the get() function to first retrieve a resource and then update it.

$ curl -X "GET"

If the item is present, the function should return the following response:

Now Consider 3rd Object to delete

Run the Command to delete Or On Clicking the DELETE Button

$ curl -X "DELETE"

A popup will show like this

On clicking on Delete the Particular instance will be deleted and give Output as


Congratulations! You’ve successfully built your first fully functional CRUD Django REST API.

Building a RESTful API can be complicated, but Django REST framework handles complexity fairly well. I hope you have fun building new APIs using the Django REST framework, and be sure to leave a comment if you have any questions. Happy coding!