torchbox/django-modelcluster — pull request #12 by timheap (2014-09-04 06:59:08+00:00)
Title: Can not access a ClusterForm with custom Media class

Accessing a `ClusterForm`s `media` property raises an error. tl;dr: `@add_metaclass` does strange things with inheritance and metaclasses, and `with_metaclass` is a better option in most cases.

The non tl;dr is that the `@add_metaclass` decorator does dirty, dirty things to your class. The initial class you define is ripped apart and reconstructed with a new metaclass by inspecting the victim classes `__dict__`. As the `media` property of forms is also created via metaclasses, and does some funky stuff with inheritance, the `super(cls, self)` call to look for parent `media` properties was failing. The `cls` variable was the original `ClusterForm` defined, before it was ripped apart and replaced with a metaclassed version - named the same, in the same variable location, but strictly a different class. This caused the `super` call to fail, as the original `ClusterForm` was not in the subclasses parent heirarchy - the replacement `ClusterForm` from `@add_metaclass` was instead.

To fix: replace `@add_metaclass` with `with_metaclass`. `@add_metaclass` is dirty.