Skip to content

Instantly share code, notes, and snippets.

@craigds
Created September 28, 2010 10:13
Show Gist options
  • Save craigds/600751 to your computer and use it in GitHub Desktop.
Save craigds/600751 to your computer and use it in GitHub Desktop.
@classmethod
def with_base_model(cls, base_model):
"""
This returns an MPTTModel CLASS with the given base model.
Used for linearizing inheritance chains.
The given model should be abstract.
"""
if cls is not MPTTModel:
raise RuntimeError, "with_base_model() must be called on MPTTModel"
if not issubclass(base_model, models.Model) or not base_model._meta.abstract:
raise ValueError, "%r is not an abstract model class" % base_model
# if we try to copy these, things break. They get set up by the metaclass soon.
to_ignore = ('__dict__', '_meta', '_mptt_meta')
# copy all attributes and methods from MPTTModel
class_dict = dict([(att, getattr(cls, att)) for att in dir(cls) if not att in to_ignore])
# call the metaclass to create our class.
return MPTTModelBase(cls.__name__, (base_model,), class_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment