Re-ordering child nodes in django-MPTT

后端 未结 1 413
感情败类
感情败类 2021-01-05 07:22

I\'m using Ben Firshman\'s fork of django-MPTT (hat tip to Daniel Roseman for the recommendation).

I\'ve got stuck trying to re-order nodes which share a common pare

相关标签:
1条回答
  • 2021-01-05 07:29

    I think this is an artefact of a failure in MPTT that I've mentioned before - when you move nodes around, it correctly updates the instance of the node you're moving, but it doesn't update the instance of the target (although it does get updated in the database).

    The consequence of this is that in your code, each m gets moved to the right of last_m - but the values in the last_m still reflect the position before the move, so the next move uses the original lft/right values instead of the new post-move ones.

    The solution is to reload last_m each time:

    for id in ids:
      last_m = MyModel.objects.get(pk=last_m.id)
      m = MyModel.get(pk = id)
      m.move_to(last_m, position='right')
    
    0 讨论(0)
提交回复
热议问题