def generate_mixin_chain(self):
mixins = []
cursor = self.__class__
while cursor is not ModelAdmin and cursor is not None:
if hasattr(cursor, 'mixins'):
mixins += [mixin for mixin in cursor.mixins if mixin not in mixins]
cursor = cursor.__base__
mixin_len = len(mixins)
if mixin_len > 0:
for index, mixin in enumerate(mixins):
target = ModelAdmin
if index != (mixin_len-1):
target = mixins[index+1]
mixin.__bases__ = (target,)
MixinModelAdmin.__bases__ = (mixins[0],)
# example usage:
class MyModelAdmin(MixinModelAdmin):
"""
The resultant inheritance tree will look like so:
ModelAdmin
DispatchableModelAdmin
WidgetModelAdmin
MixinModelAdmin
MyModelAdmin
where ModelAdmins subclass the ModelAdmin directly above them.
"""
mixins = (WidgetModelAdmin, DispatchableModelAdmin, )
Comments
Ok, so I haven't played with this and am probably missing something, but: what advantage does all this have over Plain Old Multiple Inheritance? Multiple inheritance works just fine for me in most cases (as long as super() is used properly for calling up the tree), and I've definitely used it for Models; does ModelAdmin break it in some way that this fixes?
#
I think this snipper was not copied completly... I am missing a MixinModelAdmin Class somewhere
#