2

I want to override the tree_id field as following:

Given:

class Thing(MPTTModel):
    thing_id = models.AutoField(primary_key=True)
    ...

    class MPTTMeta:
        tree_id = ?

While creating the "Thing" first parent I want to initiate tree_id with the thing_id assigned when object is firstly saved.

Next, for "Thing" objects created later I want to pass the thing_id of the first parent created earlier.

i.e. all the nodes at tree will have as tree_id the objectId(thing_id) of the first ancestor.

Is it possible?

How have I to pass the value while creating the "Thing" objects?

Edit:

So it seems the solution for my needs is merely: Thing.objects.get(pk=thing_id).get_descendants()

bentzy
  • 301
  • 2
  • 4
  • 15

1 Answers1

1

I don't know what you're trying to do. tree_id is a django-mptt internal field, it doesn't have much meaning outside the mptt algorithm.

Maybe you're trying to sort your tree by thing_id, so that things with oldest ancestors appear first in the tree? I don't know why you'd want to do that, but if so you should probably use order_insertion_by.

class MPTTMeta:
    order_insertion_by = ['thing_id']

Docs: http://django-mptt.github.com/django-mptt/models.html#model-options

craigds
  • 2,072
  • 14
  • 25
  • All I want is to select a subset of my "Things" objects. I saw at the documentation the usage of Genre.objects.all() and thought that if all my "Things" that are related to the first ancestor in the reverse-tree has as tree_id the first ancestor objectid so it will be easy select them as following: list = Thing.objects.filter(thing_type=2,tree_id=thing_id) – bentzy Mar 29 '12 at 19:06
  • So.. you're trying to get the descendants of a node? You can just use Thing.objects.get(pk=1).get_descendants() – craigds Mar 29 '12 at 19:14