Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

[Djangocon2015] Demystifying mixins with Django

1,373 views

Published on

"Demystifying mixins with Django" is a 15 minutes talk I presented at DjangoCon 2015 in Cardiff, UK.

Published in: Technology
  • Be the first to comment

[Djangocon2015] Demystifying mixins with Django

  1. 1. Demystifying Mixins with Django
  2. 2. @anabalica
  3. 3. Mixins are a controlled way of adding functionality to classes.
  4. 4. Mixins are not special language constructs.
  5. 5. In fact, mixins are ordinary Python classes. 1 class SomeMixin(object): 2 """My smart mixin""" 3 4 def test_method(self): 5 pass
  6. 6. Why use mixins? to improve modularity
  7. 7. When to use mixins? want to reuse a particular feature in a lot of different classes
  8. 8. Properties • single responsibility • not meant to be extended • not meant to be instantiated
  9. 9. In Python the concept of mixins is implemented using multiple inheritance.
  10. 10. Order matters
  11. 11. 1 class Foo(BaseFoo, SomeMixin): 2 pass base class mixin
  12. 12. 1 class Foo(BaseFoo, SomeMixin): 2 pass
  13. 13. 1 class Foo(SomeMixin, BaseFoo): 2 pass
  14. 14. 1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(TemplateView): 6 template_name = "about.html"
  15. 15. 1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(SomeMixin, TemplateView): 6 template_name = "about.html"
  16. 16. My first mixin
  17. 17. 1 # some_app/views.py 2 3 4 class LoginRequiredMixin(object): 5
  18. 18. 1 # some_app/views.py 2 3 4 class LoginRequiredMixin(object): 5 6 def dispatch(self, request, *args, **kwargs): 7
  19. 19. 1 # some_app/views.py 2 from django.core.exceptions import PermissionDenied 3 4 5 class LoginRequiredMixin(object): 6 7 def dispatch(self, request, *args, **kwargs): 8 if not request.user.is_authenticated(): 9 raise PermissionDenied 10
  20. 20. 1 # some_app/views.py 2 from django.core.exceptions import PermissionDenied 3 4 5 class LoginRequiredMixin(object): 6 7 def dispatch(self, request, *args, **kwargs): 8 if not request.user.is_authenticated(): 9 raise PermissionDenied 10 11 return super(LoginRequiredMixin, self). 12 dispatch(request, *args, **kwargs) 13
  21. 21. 1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(LoginRequiredMixin, TemplateView): 6 template_name = "about.html"
  22. 22. LoginRequiredMixin TemplateView AboutView
  23. 23. LoginRequiredMixin DetailView AboutView
  24. 24. ListView LoginRequiredListView AboutView CreateView LoginRequiredCreateView AboutView DetailView LoginRequiredDetailView AboutView FormView LoginRequiredFormView AboutView MyView LoginRequiredMyView AboutView TemplateView LoginRequiredTemplateView AboutView
  25. 25. LoginRequiredMixin TemplateView AboutView
  26. 26. dispatch() get_context_data() get_template_names() check if user is logged in, has permission add new data to the context add more flexibility to the template names
  27. 27. 1 # some_app/views.py 2 from django.views.generic import TemplateView 3 4 5 class AboutView(TemplateView): 6 template_name = "about.html"
  28. 28. dispatch() get_context_data() get_template_names() check if user is logged in, has permission add new data to the context add more flexibility to the template names
  29. 29. docs.djangoproject.com/en/1.8/ref/ class-based-views/base/
  30. 30. docs.djangoproject.com/en/1.8/ref/ class-based-views/base/
  31. 31. docs.djangoproject.com/en/1.8/topics/ class-based-views/mixins/
  32. 32. docs.djangoproject.com/en/1.8/topics/ class-based-views/mixins/
  33. 33. ccbv.co.uk/
  34. 34. django-braces Access Mixins Form Mixins Other Mixins
  35. 35. With great power comes great responsibility
  36. 36. Recap • single responsibility • plug-in functionality • isn’t creating a subtyping relation
  37. 37. Go back to your views and start writing mixins to clean up the code.

×