Code-Memo

Generic Views

Generic views in Django Rest Framework (DRF) are predefined views that handle common patterns for web APIs. They provide a way to reduce boilerplate code by offering built-in views that cover typical CRUD (Create, Read, Update, Delete) operations.

Types of Generic Views

  1. ListAPIView: To list all objects.
  2. CreateAPIView: To create a new object.
  3. RetrieveAPIView: To retrieve a specific object.
  4. UpdateAPIView: To update a specific object.
  5. DestroyAPIView: To delete a specific object.
  6. ListCreateAPIView: Combines listing and creation of objects.
  7. RetrieveUpdateAPIView: Combines retrieval and updating of objects.
  8. RetrieveDestroyAPIView: Combines retrieval and deletion of objects.
  9. RetrieveUpdateDestroyAPIView: Combines retrieval, updating, and deletion of objects.

Examples

# models.py
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    published_date = models.DateField()

    def __str__(self):
        return self.title
# serializers.py
from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'
  1. ListAPIView: To list all books.
# views.py
from rest_framework import generics
from .models import Book
from .serializers import BookSerializer

class BookList(generics.ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. CreateAPIView: To create a new book.
# views.py
class BookCreate(generics.CreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. RetrieveAPIView: To retrieve a specific book.
# views.py
class BookDetail(generics.RetrieveAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. UpdateAPIView: To update a specific book.
# views.py
class BookUpdate(generics.UpdateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. DestroyAPIView: To delete a specific book.
# views.py
class BookDelete(generics.DestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. ListCreateAPIView: To list all books and create a new book.
# views.py
class BookListCreate(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. RetrieveUpdateAPIView: To retrieve and update a specific book.
# views.py
class BookRetrieveUpdate(generics.RetrieveUpdateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. RetrieveDestroyAPIView: To retrieve and delete a specific book.
# views.py
class BookRetrieveDestroy(generics.RetrieveDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
  1. RetrieveUpdateDestroyAPIView: To retrieve, update, and delete a specific book.
# views.py
class BookRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

URL Configuration

To wire these views into the URL configuration, you can set up the urls.py file like this:

# urls.py
from django.urls import path
from .views import BookList, BookCreate, BookDetail, BookUpdate, BookDelete, BookListCreate, BookRetrieveUpdate, BookRetrieveDestroy, BookRetrieveUpdateDestroy

urlpatterns = [
    path('books/', BookList.as_view(), name='book-list'),
    path('books/create/', BookCreate.as_view(), name='book-create'),
    path('books/<int:pk>/', BookDetail.as_view(), name='book-detail'),
    path('books/<int:pk>/update/', BookUpdate.as_view(), name='book-update'),
    path('books/<int:pk>/delete/', BookDelete.as_view(), name='book-delete'),
    path('books/list-create/', BookListCreate.as_view(), name='book-list-create'),
    path('books/<int:pk>/retrieve-update/', BookRetrieveUpdate.as_view(), name='book-retrieve-update'),
    path('books/<int:pk>/retrieve-destroy/', BookRetrieveDestroy.as_view(), name='book-retrieve-destroy'),
    path('books/<int:pk>/retrieve-update-destroy/', BookRetrieveUpdateDestroy.as_view(), name='book-retrieve-update-destroy'),
]