Several generic views offered by Django Rest Framework simplify the process of building REST APIs by handling common tasks such as authentication, permission checks, and other operations.
Some of the most commonly used generic views in Django Rest Framework are:
- ListAPIView: This view displays a list of objects. It provides basic pagination and ordering functionality.
- RetrieveAPIView: This view displays a single object. It supports retrieving an object by either its primary key or by a custom lookup field.
- CreateAPIView: This view is used for creating new objects. It provides basic create and validate functionality.
- UpdateAPIView: This view is used for updating objects. It supports updating an object by either its primary key or by a custom lookup field.
- DestroyAPIView: This view is used for deleting objects. It supports deleting an object by either its primary key or custom lookup field.
- ListCreateAPIView: This view combines the functionality of ListAPIView and CreateAPIView.
- RetrieveUpdateAPIView: This view combines the functionality of RetrieveAPIView and UpdateAPIView.
- RetrieveDestroyAPIView: This view combines the functionality of RetrieveAPIView and DestroyAPIView.
- RetrieveUpdateDestroyAPIView: This view combines the functionality of RetrieveAPIView, UpdateAPIView, and DestroyAPIView.
These generic views can save a lot of time and reduce the amount of code that needs to be written when building REST APIs in Django. However, it’s important to understand that these views are just a starting point, and in many cases you’ll need to write custom views or customize the behavior of these views to meet the specific needs of your API.
Here is a small Django project that demonstrates how to use generic views in Django Rest Framework.
1. Here is a small Django project that demonstrates how to use generic views in Django Rest Framework.
$ django-admin startproject myproject $ cd myproject $ pip install djangorestframework
2. Add ‘rest_framework’ to your INSTALLED_APPS setting in settings.py:
INSTALLED_APPS = [ ... 'rest_framework',]
3. Create a Django app and add a simple model:
$ python manage.py startapp myapp
# myapp/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()
4. Run Migrations :
$ python manage.py makemigrations $ python manage.py migrate
5. Create a serializer for the Book model:
# myapp/serializers.py from rest_framework import serializers from .models import Book class BookSerializer(serializers.ModelSerializer): class Meta: model = Book fields = '__all__'
6. Create a view for the Book model using a generic view:
# myapp/views.py from rest_framework import generics from .models import Book from .serializers import BookSerializer class BookList(generics.ListCreateAPIView): queryset = Book.objects.all() serializer_class = BookSerializer class BookDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Book.objects.all() serializer_class = BookSerializer
7. Create a URL pattern for the views:
# myapp/urls.py from django.urls import path from .views import BookList, BookDetail urlpatterns = [ path('books/', BookList.as_view(), name='book-list'), path('books/<int:pk>/', BookDetail.as_view(), name='book-detail'), ]
8. Add the myapp URLs to the project’s URLs:
# myproject/urls.py from django.urls import path, include urlpatterns = [ path('api/', include('myapp.urls')), ]
9. Run the development server and test the API:
$ python manage.py runserver
You can access the API at
http://localhost:8000/api/books/ to view a list of books and
http://localhost:8000/api/books/1/ to view a specific book.
This is just a basic example to demonstrate how to use generic views in Django Rest Framework. You may need to customize the views and add additional functionality to meet the specific needs of your API.