[Django]-Django-mptt filter without breaking the tree

1๐Ÿ‘

I am having a similar issue. I would like to remove a node and all of its children.

Here is how I manage to do that:

class FolderForm(forms.ModelForm):
    class Meta:
        model = Folder
        fields = ('name', 'parent')

    def __init__(self, *args, **kwargs)
        super(FolderForm, self).__init__(*args, **kwargs)

        if self.instance is not None:
            exclude_ids = [f.id for f in self.instance.get_descendants(
                include_self=True)]

            self.fields['parent'].queryset = self.fields['parent'].queryset \
                .exclude(pk__in=exclude_ids)
๐Ÿ‘คNatim

0๐Ÿ‘

There is a simpler solution: just set all the children to hidden as well.

๐Ÿ‘คquantum

Leave a comment