আমরা django তে generics ভিউ নিয়ে কাজ করেছি যার মধ্যে ডেটাবেজ অপারেশন এর কোড দেয়া থাকে আমরা যখন ব্যবহার করি তখন সামান্য কিছু কোড দিলেই অটোমেটিক্যালি crud এর কাজ হয়ে যায় যেমন LISTVIEW এর মধ্যে model ও টেম্পলেট বলে দিলেই ডিফল্ট ভাবে লিস্ট টেম্পলেট এ শো করার কাজ ভিউটি করে দেবে ঠিক তেমনি হলো রেস্ট এর generics . প্রাক্টিক্যাল দেখি
ListCreateAPIView & RetrieveUpdateDestroyAPIView
# models.py
from django.db import models
class Post(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=255)
content = models.TextField()
active = models.BooleanField(default=False)
def __str__(self):
return self.title
Python# serializers.py
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'title', 'content', 'active']
PythonListCreateAPIView & RetrieveUpdateDestroyAPIView
এখানে দুইটি উদাহরণ দেয়া হয়েছে :
- ListCreateAPIView টি পোস্টার লিস্ট দেখাবে এবং পোস্ট তৈরী করবে
- RetrieveUpdateDestroyAPIView টি নির্দিষ্ট পোস্ট ভিউ ,এডিট ,ডিলেট এর কাজ করবে
# views.py
from rest_framework import generics, status
from rest_framework.response import Response
from .models import Post
from .serializers import PostSerializer
class PostListCreate(generics.ListCreateAPIView):
# This view handles GET (list) and POST (create) requests for the Post model
queryset = Post.objects.all()
serializer_class = PostSerializer
class PostRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView):
# This view handles GET (retrieve), PUT (update), and DELETE (destroy) requests for a single Post instance
queryset = Post.objects.all()
serializer_class = PostSerializer
def delete(self, request, *args, **kwargs):
# Custom delete method to handle DELETE request for a single Post instance
instance = self.get_object()
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
# urls.py
from django.urls import path
from .views import PostListCreate, PostRetrieveUpdateDestroy
urlpatterns = [
path('posts/', PostListCreate.as_view(), name='post-list-create'),
path('posts/<int:pk>/', PostRetrieveUpdateDestroy.as_view(), name='post-retrieve-update-destroy'),
]
ListAPIView
যদি চাই শুধু সবগুলো পোস্ট দেখতে তাহলে নিচের ভিউ ক্লাস ব্যবহার করবো
যদি চাই শুধু সবগুলো পোস্ট দেখতে তাহলে নিচের ভিউ ক্লাস ব্যবহার করবো
ListAPIView : এটা শুধু গেট মেথডে লিস্ট শো করবে
from .models import Post
from .serializers import PostSerializer
from rest_framework import generics
class PostListView(generics.ListAPIView):
queryset = Post.objects.all()
serializer_class = PostSerializer
from django.urls import path
from .views import PostListView
urlpatterns = [
path('posts/', PostListView.as_view(), name='post-list'),
]
Api Documentation
Create a new Post:
- URL:
http://localhost:8000/blogapi/posts/
- Method: POST
- Body: JSON
{
"title": "New Post",
"content": "This is a new post content.",
"active": true
}
JavaScript- Description: Create a new post with the provided title, content, and active status.
- Retrieve all Posts:
- URL:
http://localhost:8000/blogapi/posts/
- Method: GET
- Description: Retrieve all existing posts.
- URL:
- Retrieve a Post by ID:
- URL:
http://localhost:8000/blogapi/posts/<id>/
- Method: GET
- Description: Retrieve the post with the specified ID.
- URL:
- Update a Post:
- URL:
http://localhost:8000/blogapi/posts/<id>/
- Method: PUT
- Body: JSON
- URL:
{
"title": "Updated Post",
"content": "This is the updated post content.",
"active": false
}
JavaScriptDescription: Update the post with the specified ID with new title, content, and active status.
- Delete a Post:
- URL:
http://localhost:8000/blogapi/posts/<id>/
- Method: DELETE
- Description: Delete the post with the specified ID.
- URL:
Replace <id>
with the actual ID of the post you want to retrieve, update, or delete.