def register(*args, **kwargs): """ Registers a model class as an MPTTModel, adding MPTT fields and adding MPTTModel to __bases__. This is equivalent to just subclassing MPTTModel, but works for an already-created model. """ from mptt.models import MPTTModelBase return MPTTModelBase.register(*args, **kwargs)
def register(*args, **kwargs): """ Registers a model class as an MPTTModel, adding MPTT fields and adding MPTTModel to __bases__. This is equivalent to just subclassing MPTTModel, but works for an already-created model. This method was removed in 0.4.0, but restored in 0.4.2 after use-cases were reported that were impossible by merely subclassing MPTTModel. """ from mptt.models import MPTTModelBase return MPTTModelBase.register(*args, **kwargs)
from django.utils.translation import ugettext_lazy as _ from mptt.models import MPTTModelBase # enhance Group class by adding a parent field needed by mptt models.ForeignKey( Group, null=True, blank=True, related_name='children', verbose_name=_('parent'), help_text=_('The group\'s parent group. None, if it is a root node.' )).contribute_to_class(Group, 'parent') MPTTModelBase.register(Group, order_insertion_by=['name']) # enhance User class by adding a new method that returns all groups def get_all_groups(self, only_ids=False, ascending_strategy=ASCENDING_STRATEGY): """ Returns all groups the user is member of AND all descendants groups of those groups. """ direct_groups = self.groups.all() groups = set() # which direction should we really use the graph? if ascending_strategy:
from django.utils.translation import ugettext_lazy as _ from mptt.models import MPTTModelBase # enhance Group class by adding a parent field needed by mptt models.ForeignKey( Group, null = True, blank = True, related_name = 'children', verbose_name = _('parent'), help_text = _('The group\'s parent group. None, if it is a root node.') ).contribute_to_class(Group, 'parent') MPTTModelBase.register(Group, order_insertion_by=['name']) # enhance User class by adding a new method that returns all groups def get_all_groups(self, only_ids=False): """ Returns all groups the user is member of AND all descendants groups of those groups. """ direct_groups = self.groups.all() groups = set() for group in direct_groups: descendants = group.get_descendants(include_self=True).all() for descendant in descendants: if only_ids: