1

I recently had a django question - and one of the answers left me stumped:

https://stackoverflow.com/a/10608687/1061426

I've read the django doco, but cannot quite work out how mixins are relevant to what was asked, or what the answer is referring to.

So, I searched for "django mixin tutorial" and stumbled across something called dajax and dajaxice. Ok, i am lying, i came across this blog:

http://www.pythondiary.com/blog/Apr.15,2012/stay-tuned-django-ajax-tutorial.html

My question is: What are mixins? How do they relate to ajax calls? Are they used for things other than ajax? Why would i want to use dajax or dajaxice or some other django addin framework, rather than just plain django to work with ajax?

Also, there isn't a dajax tag, but there is a dajaxice tag for stackoverflow... does that mean that dajaxice is The Way To Go?

Cheers,

Community
  • 1
  • 1
bharal
  • 15,461
  • 36
  • 117
  • 195

2 Answers2

4

Mixins are a general Object Oriented Programming concept. They don't specifically have anything to do with Django or Dajax, etc. However, Django does, and Dajax probably as well, use mixins.

In general, a "mixin" is simply a class that is meant to literally be mixed in with another class. It typically doesn't do anything on its own, but rather, merely adds functionality to another class. Django's concept of "abstract" models are an example of a mixin. You never instantiate an abstract model. Instead, other models inherit from the abstract model, gaining all of its functionality, and it's those subclasses that actually are instantiated.

Django's class-based views (which is what the answer that brought you here is talking about) also use mixins. For example, most of the class-based views inherit from TemplateResponseMixin. This class is not a view itself, and you would never use it for anything but for a class-based view to inherit from. It's merely an encapsulation of all the functionality that renders a template into a response, so this functionally can be "mixed in" to all the various views without violating DRY (Don't Repeat Yourself).

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
2

No, mixins don't have anything in particular to do with Ajax.

A mixin is just a class that can be used as part of the multiple inheritance for another class. Django uses these extensively in its class-based views - some classes provide (for example) the basic functionality for displaying forms, or lists of models, and you are meant to mix those in with your own classes: create your own code that implements your own extensions to that functionality, while inheriting from one or more mixins.

I've never used Dajax, but I suppose it also uses mixins to provide the basic implementation of the Ajax handling within your views.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895