[PYTHON] Overview of class-based generic views and inherited class relationships

This article is Day 13 of Django Advent Calendar 2019. In this article, I'd like to give you an overview of Django's Class-based Generic View and its inherited class relationships.

Premise

I refer to and cite the Official Documentation of Django 3.0, the latest version at the time of this article's creation.

What is a Class-Based Generic View?

First, a "view" in Django is an encapsulation of logic that processes a user request and returns a response [^ 1]. Define the data to be displayed in the "template" which is the presentation layer, and perform processing according to the request. It's arguable, but the "controller" in the MVC framework may be closer to the general image [^ 2].

Django provides a "Class-based Generic View," which generalizes the logic of developing that view. As the name suggests, class-based generic views are class-based views, so code can be reused using inheritance [^ 3]. By utilizing the general-purpose view, you can avoid redundancy and boiler templates in Web development, and develop by making the most of Django's design concept of "Less Code" and "Don't Repeat Yourself".

List of generic views

A list and overview of generic views. The importance is set based on the frequency of personal use in normal development. I think the importance depends on the characteristics of the service.

Base view

A group of base views that define the minimum functionality of a general-purpose view. The class here is also inherited from other generic views.

class importance Overview
View ★★★ Base view of generic view
TemplateView ★★★ Base view of the view that renders the template
RedirectView ★★☆ View for redirect

View for display

A group of views for displaying the list / detail screen. I think this is the most frequent class in general website development.

class importance Overview
DetailView ★★★ View to display detail screen
ListView ★★★ View for displaying the list screen

Editing view

A group of views for displaying the edit screen. This class can be used on general CRUD operation screens.

class importance Overview
FormView ★★☆ Form screen view
CreateView ★★☆ View of creation screen
UpdateView ★★☆ Update screen view
DeleteView ★★☆ Delete screen view

Date view

It is a group of views for displaying a list by year, month, etc. like a blog.

class importance Overview
ArchiveIndexView ★☆☆ View for listing items with the latest dates
YearArchiveView ★☆☆ View for displaying the list screen by year
MonthArchiveView ★☆☆ View for displaying the monthly list screen
WeekArchiveView ★☆☆ View to display the list screen by week
DayArchiveView ★☆☆ View for displaying the daily list screen
TodayArchiveView ★☆☆ View for displaying the list screen of today's date
DateDetailView ★☆☆ View for displaying the details screen for each date

General-purpose view structure

A generic view itself inherits another generic view, or is configured with multiple inheritances of a generic mixin. A generic view defines the behavior of a view by setting its attributes and behavior. Note that most attributes have accessors defined for them, so it is possible to dynamically change the value of an attribute by overriding the accessor.

Base view

Class diagram (Django.views.generic.base)

View is the base class for all generic views, and View has attributes referenced by other classes such as request`` kwargs. TemplateView consists of multiple inheritance of TemplateResponseMixin for rendering the template file and ContextMixin which passes the context variable to the template. These two mixins are inherited by all generic views except RedirectView. base.png

View for display

Class diagram (Django.views.generic.list`` Django.views.generic.detail`)

The main difference between ListView and DetailView is the Mixin surrounded by a red frame. You can see that the basic structure is almost the same, only the difference depends on whether the context variable passed to the template is a list or a single object. list.png detail.png

Editing view

Class diagram (Django.views.generic.edit)

Since there are many inherited classes, only the general view and mixin in the same module are surrounded by a red frame. All general-purpose views except DeleteView inherit from FormMixin which generates forms and ProcessFormView which performs rendering at GET and validation at POST. For the remaining views and Mixins, the classes based on the CRUD operations associated with each editing view are inherited. edit.png

Finally

I think it's easy for people who are new to Django to get a feel for the generalization of generalized views of Django, understand classes and mixins and their attributes (fields), and behaviors (methods). There are many good articles on how-tos for class-based general-purpose views, so I tried to get a graphical overview of this article.

Recommended Posts

Overview of class-based generic views and inherited class relationships
Getting started and using sample class-based generic views in Django
Calculation of homebrew class and existing class
django class-based views API class diagram
Example of using class variables and class methods
[Python] Class type and usage of datetime module
Summary of pickle and unpickle processing of user-defined class