Rewriting our API using class-based views
We’ll start by rewriting the root view as a class-based view. All this involves is a little bit of refactoring ofviews.py.
snippets/views.py
views.py.
snippets/views.py
snippets/urls.py slightly now that we’re using class-based views.
snippets/urls.py
Using mixins
One of the big wins of using class-based views is that it allows us to easily compose reusable bits of behavior. The create/retrieve/update/delete operations that we’ve been using so far are going to be pretty similar for any model-backed API views we create. Those bits of common behavior are implemented in REST framework’s mixin classes. Let’s take a look at how we can compose the views by using the mixin classes. Here’s ourviews.py module again.
snippets/views.py
GenericAPIView, and adding in ListModelMixin and CreateModelMixin.
The base class provides the core functionality, and the mixin classes provide the .list() and .create() actions. We’re then explicitly binding the get and post methods to the appropriate actions. Simple enough stuff so far.
snippets/views.py
GenericAPIView class to provide the core functionality, and adding in mixins to provide the .retrieve(), .update() and .destroy() actions.
Understanding the Mixin Pattern
ListModelMixin
Provides a
.list() method that returns a list of all instances from the queryset.CreateModelMixin
Provides a
.create() method that creates and saves a new model instance.RetrieveModelMixin
Provides a
.retrieve() method that returns a single instance from the queryset.UpdateModelMixin
Provides a
.update() method that updates and saves an existing model instance.DestroyModelMixin
Provides a
.destroy() method that deletes an existing model instance.Using generic class-based views
Using the mixin classes we’ve rewritten the views to use slightly less code than before, but we can go one step further. REST framework provides a set of already mixed-in generic views that we can use to trim down ourviews.py module even more.
snippets/views.py
Available Generic Views
REST framework provides several generic views:ListAPIView
ListAPIView
Used for read-only endpoints to represent a collection of model instances. Provides a
get method handler.CreateAPIView
CreateAPIView
Used for create-only endpoints. Provides a
post method handler.RetrieveAPIView
RetrieveAPIView
Used for read-only endpoints to represent a single model instance. Provides a
get method handler.UpdateAPIView
UpdateAPIView
Used for update-only endpoints for a single model instance. Provides
put and patch method handlers.DestroyAPIView
DestroyAPIView
Used for delete-only endpoints for a single model instance. Provides a
delete method handler.ListCreateAPIView
ListCreateAPIView
Used for read-write endpoints to represent a collection of model instances. Provides
get and post method handlers.RetrieveUpdateAPIView
RetrieveUpdateAPIView
Used for read or update endpoints to represent a single model instance. Provides
get, put and patch method handlers.RetrieveDestroyAPIView
RetrieveDestroyAPIView
Used for read or delete endpoints to represent a single model instance. Provides
get and delete method handlers.RetrieveUpdateDestroyAPIView
RetrieveUpdateDestroyAPIView
Used for read-write-delete endpoints to represent a single model instance. Provides
get, put, patch and delete method handlers.Benefits of Class-Based Views
Code Reusability
Common patterns like listing and creating objects can be implemented once and reused across your API.
Better Organization
HTTP methods are separated into distinct methods (get, post, put, delete), making the code more maintainable.
Easy Customization
You can override specific methods to customize behavior while keeping the rest of the default implementation.
