0

I have encountered an strange problem with the use of TreeManager

Here is my code:

# other imports
from mptt.models import MPTTModel, TreeForeignKey
from mptt.managers import TreeManager

class SectionManager(TreeManager):
    def get_queryset(self):
       return super().get_queryset().filter(published=True)

class Section(MPTTModel):
    published = models.BooleanField(
    default=True,
    help_text="If unpublished, this section will show only"
              " to editors. Else, it will show for all."
)

    objects = TreeManager()
    published_objects = SectionManager()

When I test it. I get the following correct results:

# show all objects
Section.objects.count()  # result is correct - 65
Section.objects.root_nodes().count() # result is correct - 12

# show published objects, just one is not published.
Section.published_objects.count() # result is correct - 64
Section.published_objects.root_nodes().count()  # result is corrct - 12

But one child of the roots is unpublished and it does not show in the results. Here is the test:

for root in Section.objects.root_nodes(): 
    print(f"root_section_{root.id} has {root.get_children().count()} children") 

    # results ...
    root_section_57 has 13 children # correct - 13 items
    # ... more results

for root in Section.published_objects.root_nodes(): 
    print(f"root_section_{root.id} has {root.get_children().count()} children") 

    # results ...
    root_section_57 has 13 children # WRONG - should be only 12 children
    # ... more results

I may not understand something, or I may have hit a bug?? Any ideas?

NOTE: This issue has been posted on the django-mptt github issues page at: https://github.com/django-mptt/django-mptt/issues/689

joe
  • 8,383
  • 13
  • 61
  • 109
jcuot
  • 935
  • 9
  • 24

1 Answers1

0

https://github.com/django-mptt/django-mptt/blob/master/mptt/managers.py

You override wrong function call. root_nodes() call ._mptt_filter()

    @delegate_manager
    def root_nodes(self):
        """
        Creates a ``QuerySet`` containing root nodes.
        """
        return self._mptt_filter(parent=None)

And your _mptt_filter does not got any given qs.

    @delegate_manager
    def _mptt_filter(self, qs=None, **filters):
        """
        Like ``self.filter()``, but translates name-agnostic filters for MPTT
        fields.
        """
        if qs is None:
            qs = self
        return qs.filter(**self._translate_lookups(**filters))

Now you need to customized based on your use case.

Hope it can be some help

joe
  • 8,383
  • 13
  • 61
  • 109