Generic API View and Mixins in Django REST Framework (English) | GenericAPIView and MIXINS in DRF

Published: 24 November 2023
on channel: Coding Is Thinking
654
9

Django REST Framework Views - Generic Views:
In this part, we will look into DRF’s Generic Views and Mixin classes. GenericAPIView class extends based on the APIView class.

Most of the time when we write a web application we need to deal with a database. Some functions will add values in DB and some will remove them. One of the key benefits of class-based views is the way they allow you to compose bits of reusable behavior. So to reduce the amount of boilerplate code that we need to write for some frequently used views that are mostly mapped to DB models, Django provides Generic View classes.

Most of the common functionalities are already written in these classes. These are the combinations of GenericAPIView class and some Mixin class. We can use them to create our views.
End goal

In this series we will look into all the different ways we can use DRF to write views. In this tutorial series, I will cover all the three view classes of DRF:

APIView
Generic Views(current post)
ViewSets

We will NOT build an end-to-end web application but we will assume scenarios, whenever the need occurs, to understand the concepts.

We will also look at how we can test our code using Django’s native testing tools and also pytest(this part will be covered in the next and final part of this series, as testing does not have any dependency on which classes we use to create our views).
Generic Views — Overview

The word generic suggests a lot about the scope of these classes, it means that it has features for a lot of similar things. Generic views are commonly used patterns. Let’s take blogging apps as an example, there might be thousands of blogging site and all of them has at least one common feature, a page that lists all of the latest posts. So most sites are writing this view in more or less the same way. Here come the Generic view classes, it is written to make your code smaller for list and detail views. As it is inherited from APIView Generic views have got all of its features too.

Their purpose is for you to quickly build API views that map closely to your database models without repeating yourself.

It consists of GenericAPIView, Mixins, and Concrete Views:

GenericAPIView is a more loaded version of APIView. It isn't really useful on its own but can be used to create reusable actions.
Mixins have common behaviors, such as list, create, etc. These are generally not used without GenericAPIView .
Concrete views combine GenericAPIView, with the appropriate mixins. This is what we mostly deal with if we are using Generic Views. It provides the appropriate level of abstraction, but if you want more flexibility then you can use GenericAPIView , Mixin classes of your choice.

GenericAPIView

This is the base class in all the Generic View classes. This class has methods such as get_queryset(self) , filter_queryset(self, queryset) etc. We will talk about these methods later, first, let us take a look at some important attributes of this class.
Attributes:

1. queryset: This should be used for returning objects from this view. Typically, you must either set this attribute or override the get_queryset() method. If you are overriding a view method, it is important that you call get_queryset() instead of accessing this property directly, as queryset will get evaluated once, and those results will be cached for all subsequent requests.

Keywords:
Generic API View and Mixins in Django REST Framework (Hindi)
Intermediate Level - Django Rest Framework Lesson 2 (GenericAPIView & Introduction Mixins)
15 GenericAPIView and Mixins | Django Rest Framework
Django REST framework: Concepts behind View, APIView, GenericAPIView, ViewSets and ModelMixins
Django Mixins CRUD Operations in Rest Framework
6 | Model mixins and generic API views | By Hardik Patel
21. Mixins | Django Rest framework 🔥
Django Rest API CRUD | Class Based Views | Generic, APIView & Mixin Classes | Django Rest Framework

Hashtags:
#programming #coding #programmer #python #developer #javascript #technology #code #java #coder #html #computerscience #software #tech #css #webdeveloper #webdevelopment


Watch video Generic API View and Mixins in Django REST Framework (English) | GenericAPIView and MIXINS in DRF online, duration hours minute second in high quality that is uploaded to the channel Coding Is Thinking 24 November 2023. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 654 times and liked it 9 visitors.