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 manage.py startapp core

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

python manage.py migrate

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

# settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'core'
]

Create a model in models.py file like

from django.db import models

# Create your models here.


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


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

Then apply Commands and Create a SuperUser to Our project:

python manage.py makemigrations
python manage.py migrate
python manage.py 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)
        drinks.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

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

Run the Django server:

python manage.py runserver

Now, we’re ready for the first test. Navigate to http://127.0.0.1:8000. 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" http://127.0.0.1:8000/3

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" http://127.0.0.1:8000/3

A popup will show like this

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

Conclusion

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!