Skip to content

Use Django-MPTT instead of home brewed tree functions#183

Open
AidanCurrah wants to merge 1 commit intodevelopfrom
feature-MPTTUpdates
Open

Use Django-MPTT instead of home brewed tree functions#183
AidanCurrah wants to merge 1 commit intodevelopfrom
feature-MPTTUpdates

Conversation

@AidanCurrah
Copy link
Copy Markdown
Contributor

This migrates the CMS to use Django MPTT and it's helper classes/functions for our tree structure and allows us to get rid of some overly complex functions.

set_page_child_levels(homepage, 0)

def reverse_page_tree_level(apps, schema_editor):
# We don't have anything here are we're removing the 'level' field anyway
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That worries me. Is there absolutely no way of reversing this migration?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have to have anything. The forwards migration just populates the new 'level' field that gets added with this migration.
When going backwards we don't need to do anything since the field is getting removed anyway.

Comment thread cms/apps/pages/models.py
Comment on lines +176 to +185
@cached_property
def children(self):
'''The child pages for this page.'''
children = []
if self.right - self.left > 1: # Optimization - don't fetch children
# we know aren't there!
for child in self.child_set.filter(is_content_object=False):
child.parent = self
children.append(child)
return children
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the get_children() method of MPTTModel not work here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would. I might need to include the filter on it though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However using get_children() avoids the slight optimisation of having children.parent being the current instance and avoiding a DB lookup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants