0

I use Django MPTT.

I wont delete my nodes but instead I am updating a field is_deleted and filtering the nodes so 'deleted' nodes wont show up.

The problem arises if a node is 'deleted' but some of it's descendants is not deleted.

The descendants of a 'deleted' node should not be showed. I could solve this by setting the is_deleted field for all descendants when I 'delete' a node but that is not the best solution.

How can this be solved?

Jamgreen
  • 10,329
  • 29
  • 113
  • 224

1 Answers1

0

Simply set is_deleted to first level (root) objects some_object.get_root(), then get descendances of all objects in this queryset SomeModel.objects.filter(level=0).exclude(is_deleted=True) django-mptt get_descendants for a list of nodes

Community
  • 1
  • 1
madzohan
  • 11,488
  • 9
  • 40
  • 67
  • I am not interested in setting `is_deleted` for only the root node. Sometimes it's the root node which I am going to delete but other times it's nodes on other levels. – Jamgreen Oct 21 '14 at 20:04
  • Then set `is_deleted=True` to deleted objects, then `black_list = get_queryset_descendants(SomeModel.objects.filter(is_deleted=True), include_self=True)` you will have list of deleted objects with their descendants, Finally you have to exclude them all `SomeModel.objects.exclude(pk__in=[o.id for o in black_list])` – madzohan Oct 21 '14 at 20:25