Using mixins for class-based views

Mixins are a special kind of multiple inheritance for a class. You can use them to provide common discrete functionality that, added to other mixins, allows you to define the behavior of a class. There are two main situations to use mixins:

  • You want to provide multiple optional features for a class
  • You want to use a particular feature in several classes

Django comes with several mixins that provide additional functionality to your class-based views. You can learn more about mixins at https://docs.djangoproject.com/en/2.0/topics/class-based-views/mixins/.

We are going to create a mixin class that includes a common behavior and use it for the course's views. Edit the views.py file of the courses application and modify it as follows:

from django.urls import reverse_lazy
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView,
DeleteView
from .models import Course

class OwnerMixin(object):
def get_queryset(self):
qs = super(OwnerMixin, self).get_queryset()
return qs.filter(owner=self.request.user)

class OwnerEditMixin(object):
def form_valid(self, form):
form.instance.owner = self.request.user
return super(OwnerEditMixin, self).form_valid(form)

class OwnerCourseMixin(OwnerMixin):
model = Course

class OwnerCourseEditMixin(OwnerCourseMixin, OwnerEditMixin):
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
template_name = 'courses/manage/course/form.html'

class ManageCourseListView(OwnerCourseMixin, ListView):
template_name = 'courses/manage/course/list.html'

class CourseCreateView(OwnerCourseEditMixin, CreateView):
pass

class CourseUpdateView(OwnerCourseEditMixin, UpdateView):
pass

class CourseDeleteView(OwnerCourseMixin, DeleteView):
template_name = 'courses/manage/course/delete.html'
success_url = reverse_lazy('manage_course_list')

In this code, we create the OwnerMixin and OwnerEditMixin mixins. We will use these mixins together with the ListView, CreateView, UpdateView, and DeleteView views provided by Django. OwnerMixin implements the following method:

  • get_queryset(): This method is used by the views to get the base QuerySet. Our mixin will override this method to filter objects by the owner attribute to retrieve objects that belong to the current user (request.user).

OwnerEditMixin implements the following method:

  • form_valid(): This method is used by views that use Django's ModelFormMixin mixin, that is, views with forms or modelforms such as CreateView and UpdateView. form_valid() are executed when the submitted form is valid. The default behavior for this method is saving the instance (for modelforms) and redirecting the user to success_url. We override this method to automatically set the current user in the owner attribute of the object being saved. By doing so, we set the owner for an object automatically when it is saved.

Our OwnerMixin class can be used for views that interact with any model that contains an owner attribute.

We also define an OwnerCourseMixin class that inherits OwnerMixin and provides the following attribute for child views:

  • model: The model used for QuerySets. Used by all views.

We define a OwnerCourseEditMixin mixin with the following attributes:

  • fields: The fields of the model to build the model form of the CreateView and UpdateView views.
  • success_url: Used by CreateView and UpdateView to redirect the user after the form is successfully submitted. We use a URL with the name manage_course_list that we are going to create later.

Finally, we create the following views that subclass OwnerCourseMixin:

  • ManageCourseListView: Lists the courses created by the user. It inherits from OwnerCourseMixin and ListView.
  • CourseCreateView: Uses a modelform to create a new Course object. It uses the fields defined in OwnerCourseEditMixin to build a model form and also subclasses CreateView.
  • CourseUpdateView: Allows editing an existing Course object. It inherits from OwnerCourseEditMixin and UpdateView.
  • CourseDeleteView: Inherits from OwnerCourseMixin and the generic DeleteView. Defines success_url to redirect the user after the object is deleted.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset