JianWei
JianWei

Reputation: 131

attribute override concept in multiple inheritance (Django-Mixin)

Please refer to following code:

from django.views.generic.edit import 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 OwnerCourseMixin(OwnerMixin):
    model = Course

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

I could easily understand that by adding template_name and success_url attribute in CourseDeleteView, I could override the attribute in DeleteView. So that the two attributes take effect by calling the method in DeleteView.

What confused me is why could I transfer the attribute model = Course in OwnerCourseMixin to DeleteView by the code above. There's no direct inheritance-relationship between them. It makes no sense to me.

Upvotes: 1

Views: 451

Answers (1)

lucasnadalutti
lucasnadalutti

Reputation: 5948

You don't transfer the model attribute to DeleteView, but to CourseDeleteView only, since this is the class that inherits from OwnerCourseMixin.

If you instantiated DeleteView, that instance wouldn't have model = Course since, as you cleverly stated, there's no direct inheritance between them. However, an instance of CourseDeleteView would have model = Course, which it inherited from OwnerCourseMixin.

Upvotes: 1

Related Questions