Issue 35: How can I regenerate the hidden fields value having only the parent_id?
Status:  Verified
Owner: ----
Closed:  Sep 2010
Reported by batiste....@gmail.com, Nov 25, 2008
Hello,

I have to migrate a hierarchy from another PHP project and I want to use
Django mptt.

I have an ID and a Parent ID. I insert the data into the data base with
"lft", "rght", "level", "tree_id" set to 0. (Via SQL inserts)

How can I regenerate correct values for the hidden fields? If I go into the
admin, edit my Category object these values stay to 0. Does mppt have a
magic method that regenerate the all tree in database? How can I do that?

Thy a lot for you help,
Batiste Bieler
Nov 25, 2008
#1 batiste....@gmail.com
It seems that simply save my model again do the trick... Thx for mptt

from myapp.models import Category
c = Category(id=0, order=1).save()

for c in Category.objects.all():
    if c.parent_id == 0:
        c.parent = None
    c.save()
    
c.delete()
Nov 27, 2008
#2 batiste....@gmail.com
In fact I was wrong, it's not working:

>>> category.get_descendants()
[]
>>> category.children.all()
[<Category: cat1>, ..., <Category: cat6>]

Any idea how to fix that?
Dec 5, 2008
#3 mocksoul
Patch in  issue#13  (by me) adds a magic Model.tree.rebuild() method for you :).
Dec 5, 2008
#4 batiste....@gmail.com
Hey man, thanks lot!
Sep 2, 2010
Project Member #5 craig.ds@gmail.com
(No comment was entered for this change.)
Status: Verified