Skip to content

Commit

Permalink
Merge pull request #743 from ncopiy/allow-to-use-tree-queryset-as-man…
Browse files Browse the repository at this point in the history
…ager

Allow to use TreeQuerySet as manager method to get TreeManager class
  • Loading branch information
matthiask committed Jul 23, 2020
2 parents cbd7ff2 + fdb2971 commit c58163d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ MANIFEST
tests/mydatabase
*.egg
.eggs
venv/
.idea/
9 changes: 9 additions & 0 deletions mptt/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@


class TreeQuerySet(models.query.QuerySet):
def as_manager(cls):
# Address the circular dependency between `Queryset` and `Manager`.
from mptt.managers import TreeManager
manager = TreeManager.from_queryset(cls)()
manager._built_with_as_manager = True
return manager
as_manager.queryset_only = True
as_manager = classmethod(as_manager)

def get_descendants(self, *args, **kwargs):
"""
Alias to `mptt.managers.TreeManager.get_queryset_descendants`.
Expand Down
6 changes: 6 additions & 0 deletions tests/myapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from django.contrib.admin.views.main import ChangeList
from django.contrib.admin import ModelAdmin, site
from mptt.admin import TreeRelatedFieldListFilter
from mptt.querysets import TreeQuerySet

try:
from mock_django import mock_signal_receiver
Expand Down Expand Up @@ -1732,6 +1733,11 @@ def test_get_descendants(self):
Category.objects.filter(name="Nintendo Wii").get_descendants(include_self=True)],
)

def test_as_manager(self):
self.assertTrue(
issubclass(TreeQuerySet.as_manager().__class__, TreeManager)
)


class TreeManagerTestCase(TreeTestCase):

Expand Down

0 comments on commit c58163d

Please sign in to comment.