Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3217844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:27:31+00:00 2026-05-17T15:27:31+00:00

I’m trying to make a copy of a FeinCMS page tree, which is managed

  • 0

I’m trying to make a copy of a FeinCMS page tree, which is managed using django-mptt. I wrote this function:

def make_tree_copy(page, parent=None):
    '''
    Makes a copy of the tree starting at "page", reparenting it to "parent"
    '''
    new_page = Page.objects.create_copy(page)
    new_page.save()
    Page.tree.move_node(new_page, parent)

    # re-read so django-mptt fields get updated
    new_page = Page.objects.get(id=new_page.id)
    for child in page.get_children():
        # re-read so django-mptt fields get updated
        child = Page.objects.get(id=child.id)
        make_tree_copy(child, new_page)

and call it using

make_tree_copy(Page.tree.root_nodes()[0])

It works in general but when I have a page tree looking like this:

A
|- B
   |- C
   |- D

It comes out as this:

A
|- B
   |- D
   |- C

From my stepping through the mptt code, the magic seems to happen in mptt/managers.py/_inter_tree_move_and_close_gap(), where for some reason the “lft” values of the grandchildren get changed. Before the move they are C=3, D=5, afterwards they are C=5, D=3.

Which explains why D gets sorted before C but I have no idea why these values get switched. Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:27:31+00:00Added an answer on May 17, 2026 at 3:27 pm

    Ok, I knew once I ask – I’d find the answer myself (after spending hours before…) Of course it’s the same problem as in all the other django-mptt problems on StackOverflow: you have to re-read the object from the database.

    I did so in the snippet above but at the wrong places. This is the code that works (re-reading the parent on entering the recursive function):

    def make_tree_copy(page, parent=None):
        '''
        Makes a copy of the tree starting at "page", reparenting it to "parent"
        '''
        if parent:
            # re-read so django-mptt fields get updated
            parent = Page.objects.get(id=parent.id)
    
        new_page = Page.objects.create_copy(page)
        new_page.save()
        Page.tree.move_node(new_page, parent)
    
        for child in page.get_children():
            make_tree_copy(child, new_page)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.