Skip to content

Commit

Permalink
Fixes #356 - Django 1.8 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
craigds committed Feb 11, 2015
1 parent 9fc198c commit 3ebca5b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
19 changes: 2 additions & 17 deletions mptt/managers.py
Expand Up @@ -11,6 +11,7 @@

from mptt.exceptions import CantDisableUpdates, InvalidMove
from mptt.querysets import TreeQuerySet
from mptt.utils import _get_tree_model

__all__ = ('TreeManager',)

Expand Down Expand Up @@ -65,23 +66,7 @@ def contribute_to_class(self, model, name):
super(TreeManager, self).contribute_to_class(model, name)

if not (model._meta.abstract or model._meta.proxy):
# Find the model that contains the tree fields.
# This is a weird way of going about it, but Django doesn't let us access
# the fields list to detect where the tree fields actually are,
# because the app cache hasn't been loaded yet.
# So, it *should* be the *last* concrete MPTTModel subclass in the mro().
bases = list(model.mro())
while bases:
b = bases.pop()
# NOTE can't use `issubclass(b, MPTTModel)` here because we can't import MPTTModel yet!
# So hasattr(b, '_mptt_meta') will have to do.
if hasattr(b, '_mptt_meta') and not (b._meta.abstract or b._meta.proxy):
break
else:
# this shouldn't ever happen! (fingers crossed ;)
raise TypeError("Couldn't find a concrete MPTTModel subclass in %s.mro()" % model)

self.tree_model = b
self.tree_model = _get_tree_model(model)

self._base_manager = None
if self.tree_model is not model:
Expand Down
9 changes: 4 additions & 5 deletions mptt/models.py
Expand Up @@ -13,6 +13,7 @@

from mptt.fields import TreeForeignKey, TreeOneToOneField, TreeManyToManyField
from mptt.managers import TreeManager
from mptt.utils import _get_tree_model


__all__ = (
Expand Down Expand Up @@ -307,11 +308,9 @@ def register(meta, cls, **kwargs):
bases.insert(0, MPTTModel)
cls.__bases__ = tuple(bases)

for key in ('left_attr', 'right_attr', 'tree_id_attr', 'level_attr'):
field_name = getattr(cls._mptt_meta, key)
try:
cls._meta.get_field(field_name)
except models.FieldDoesNotExist:
if _get_tree_model(cls) is cls:
for key in ('left_attr', 'right_attr', 'tree_id_attr', 'level_attr'):
field_name = getattr(cls._mptt_meta, key)
field = models.PositiveIntegerField(db_index=True, editable=False)
field.contribute_to_class(cls, field_name)

Expand Down
16 changes: 16 additions & 0 deletions mptt/utils.py
Expand Up @@ -175,3 +175,19 @@ def print_debug_info(qs, file=None):
row.append(getattr(n, field))
row.append('%s%s' % ('- ' * level, text_type(n).encode('utf-8')))
writer.writerow(row)


def _get_tree_model(model_class):
# Find the model that contains the tree fields.
# This is a weird way of going about it, but Django doesn't let us access
# the fields list to detect where the tree fields actually are,
# because the app cache hasn't been loaded yet.
# So, it *should* be the *last* concrete MPTTModel subclass in the mro().
bases = list(model_class.mro())
while bases:
b = bases.pop()
# NOTE can't use `issubclass(b, MPTTModel)` here because we can't import MPTTModel yet!
# So hasattr(b, '_mptt_meta') will have to do.
if hasattr(b, '_mptt_meta') and not (b._meta.abstract or b._meta.proxy):
return b
return None

0 comments on commit 3ebca5b

Please sign in to comment.