
Viewsets are a class in Django Rest Framework (DRF) that provide a simple, yet powerful way to control how your API views behave. They are part of the View Layer in DRF, which sits between the URL routing and the serialization. The ViewSets are defined using class-based views and can be used to define the basic CRUD operations for a model-based API, such as list, create, retrieve, update, and delete.
For example, here’s a simple ViewSet for a model called Book
:
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
With this ViewSet, you can perform all CRUD operations on the Book
model without having to write a separate view for each operation. You can use the built-in views provided by DRF or create your custom views as needed.
Additionally, ViewSets allow you to easily customize the behavior of your views by using mixins and overriding methods, such as the get_queryset
method to control the queryset used for the list view, or the create
method to add custom behavior to the create view.
Overall, ViewSets provide a convenient and concise way to handle your API views and can help to keep your code organized and maintainable.
Here’s an example of a simple Django project that uses ViewSets in DRF:
1. First, start by creating a new Django project:
$ django-admin startproject simple_project
2. Next, create a new Django app within the project:
$ cd simple_project
$ python manage.py startapp books
3. Then, install the Django Rest Framework using pip:
$ pip install djangorestframework
4. In your settings.py
file, add the following line to the INSTALLED_APPS list:
INSTALLED_APPS = [ ... 'rest_framework', ...]
5 . In your books
app, create a new model called Book
with the following fields:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
description = models.TextField()
publication_date = models.DateField()
6. Then, create a serializer for the Book
model:
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title', 'author', 'description', 'publication_date']
7. Create a ViewSet for the Book
model:
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
8. Finally, create the URL routing for the API by adding the following code to your urls.py
file:
from django.urls import path, include
from rest_framework import routers
from .views import BookViewSet
router = routers.DefaultRouter()
router.register(r'books', BookViewSet)
urlpatterns = [
path('', include(router.urls)),
]
With these steps, you now have a simple Django project that uses ViewSets in DRF to perform CRUD operations on the Book
model. You can run the development server using the following command
$ python manage.py runserver
And access the API at http://localhost:8000/
. You should be able to view, create, update, and delete books using the API.