google-code-export / django-mptt

Automatically exported from code.google.com/p/django-mptt
Other
0 stars 0 forks source link

How can I regenerate the hidden fields value having only the parent_id? #35

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by batiste....@gmail.com on 25 Nov 2008 at 2:06

GoogleCodeExporter commented 9 years ago
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()

Original comment by batiste....@gmail.com on 25 Nov 2008 at 2:19

GoogleCodeExporter commented 9 years ago
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?

Original comment by batiste....@gmail.com on 27 Nov 2008 at 9:44

GoogleCodeExporter commented 9 years ago
Patch in issue#13 (by me) adds a magic Model.tree.rebuild() method for you :).

Original comment by mocksoul on 6 Dec 2008 at 12:19

GoogleCodeExporter commented 9 years ago
Hey man, thanks lot!

Original comment by batiste....@gmail.com on 6 Dec 2008 at 1:21

GoogleCodeExporter commented 9 years ago

Original comment by craig.ds@gmail.com on 2 Sep 2010 at 12:23