Create A Crud Api In Four Steps Using Djangorestframework
- Shivam Rohilla
- Jan. 4, 2023
- 3 comments
- 301 Views
<meta name="clckd" content="6301244fb7ed470eb1cd6b8c31d01ac4" />
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.
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
Once you have them installed, create a new Django project and app:
django-admin startproject myproject cd myproject python manage.py startapp myapp
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)
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')
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
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'), ]
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
My social media links