MPTT - How to override “tree_id”
-
27-05-2021 - |
문제
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()
해결책
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