Ana Balica @anabalica slides - http://www.slideshare.net/AnaBalica/djangocon2015-demystifying-mixins-with-django
Say copy/paste a feature between classes that are otherwise different
class MyMixin(object):
def mymethod(self):
pass
class Foo(BaseFoo, MyMixin):
# read as MyMixin is the base class, extended by BaseFoo, extended by Foo
# so this is bad
pass
class Foo(MyMixin, BaseFoo):
# better
pass
extensions goes from right to left
Class Based Views example
class LoginRequiredMixin(object):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
raise PermissionDenied
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
class AboutView(LoginRequiredMixin, TemplateView):
template_name = "about.html"
Useful methods in class based views
How to learn
Summary