Create a CRUD API in four steps using DjangoRestFramework.

shivamrohilla

Shivam Rohilla

Posted on January 3, 2023

Create a CRUD API in four steps using DjangoRestFramework.

DjangoRestFramework (DRF) is a powerful toolkit for building APIs in the Django web framework. It provides a set of tools and libraries for building APIs, including serialization, validation, authentication, and more. DRF is designed to make it easy for developers to create APIs that can be consumed by a variety of clients, including web and mobile applications. With DRF, developers can build APIs that support CRUD (create, read, update, delete) operations, as well as custom endpoints for specific functionality. Overall, DRF is a popular choice for building APIs in Django due to its flexibility and ease of use.

Post link:- 

https://pythondjangogeek.com/django/create-a-crud-api-in-four-steps-using-djangorestfr/

Enter fullscreen mode Exit fullscreen mode

CRUD API, or Create, Read, Update, and Delete API, allows developers to create an API that performs basic database operations. In this blog, we will go through the steps of creating a CRUD API using DjangoRestFramework in four simple steps.

Setup Django and Django Rest Framework:

First, make sure you have Django and Django Rest Framework installed on your machine. If you don't have them installed, you can install them using pip:

pip install django
pip install djangorestframework
Enter fullscreen mode Exit fullscreen mode

Once you have them installed, create a new Django project and app:

django-admin startproject myproject
cd myproject
python manage.py startapp myapp
Enter fullscreen mode Exit fullscreen mode

Create a Model:
In this step, we will create a model that represents the object that we want to store in our API. For example, let's say we want to store a list of tasks. We can create a Task model with the following fields:

from django.db import models

class Task(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    completed = models.BooleanField(default=False)
Enter fullscreen mode Exit fullscreen mode

Create a Serializer and a View:

Next, we need to create a serializer and a view to handle the incoming HTTP requests. A serializer is used to convert the data in our model into a JSON format that can be sent over the internet.

from rest_framework import serializers
from .models import Task

class TaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = Task
        fields = ('id', 'title', 'description', 'completed')
Enter fullscreen mode Exit fullscreen mode

The view will handle the incoming HTTP requests and return a response. We can create a view using Django Rest Framework's generic views:

from rest_framework import generics
from .models import Task
from .serializers import TaskSerializer

class TaskList(generics.ListCreateAPIView):
    queryset = Task.objects.all()
    serializer_class = TaskSerializer

class TaskDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Task.objects.all()
    serializer_class = TaskSerializer

Enter fullscreen mode Exit fullscreen mode

Create a URL pattern:
Finally, we need to create a URL pattern that maps the incoming HTTP requests to our view. Add the following to your urls.py file:

from django.urls import path
from . import views

urlpatterns = [
    path('tasks/', views.TaskList.as_view(), name='task_list'),
    path('tasks/<int:pk>/', views.TaskDetail.as_view(), name='task_detail'),
]

Enter fullscreen mode Exit fullscreen mode

That's it! With these four steps, you have created a CRUD API using Django Rest Framework. You can test your API using a tool
like Postman or by making HTTP requests using Python's requests library.

Shivam Rohilla | Python Developer
Thank You

💖 💪 🙅 🚩
shivamrohilla
Shivam Rohilla

Posted on January 3, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related