Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. # other imports
  2. from mptt.models import MPTTModel, TreeForeignKey
  3. from mptt.managers import TreeManager
  4.  
  5. class SectionManager(TreeManager):
  6. def get_queryset(self):
  7. return super().get_queryset().filter(published=True)
  8.  
  9. class Section(MPTTModel):
  10. published = models.BooleanField(
  11. default=True,
  12. help_text="If unpublished, this section will show only"
  13. " to editors. Else, it will show for all."
  14. )
  15.  
  16. objects = TreeManager()
  17. published_objects = SectionManager()
  18.  
  19. # show all objects
  20. Section.objects.count() # result is correct - 65
  21. Section.objects.root_nodes().count() # result is correct - 12
  22.  
  23. # show published objects, just one is not published.
  24. Section.published_objects.count() # result is correct - 64
  25. Section.published_objects.root_nodes().count() # result is corrct - 12
  26.  
  27. for root in Section.objects.root_nodes():
  28. print(f"root_section_{root.id} has {root.get_children().count()} children")
  29.  
  30. # results ...
  31. root_section_57 has 13 children # correct - 13 items
  32. # ... more results
  33.  
  34. for root in Section.published_objects.root_nodes():
  35. print(f"root_section_{root.id} has {root.get_children().count()} children")
  36.  
  37. # results ...
  38. root_section_57 has 13 children # WRONG - should be only 12 children
  39. # ... more results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement