Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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()
- # 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
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement