Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting recursion #13

Open
mvdwaeter opened this issue Dec 18, 2013 · 1 comment
Open

Deleting recursion #13

mvdwaeter opened this issue Dec 18, 2013 · 1 comment

Comments

@mvdwaeter
Copy link
Contributor

When I delete a node that has children in the django-admin, I get a ForeignKey constrain IntegrityError, resulting in not deleting anything (using Postgres).

I think this is a flaw in the deleting sequence. What I think happens is this.
Take the Tree structure:

- Edition
-- Chapter
--- ContentPage
---- Module
  • delete a Chapter
  • this node has content-pages, hence remove these first
  • when trying to remove the content-page, it can't because there are modules refering to it

I (temp) fixed it with a pre_save signal like so:

from django.db.models.signals import pre_delete
from django.dispatch import receiver

@receiver(pre_delete)
def pre_delete(sender, instance, using, **kwargs):

    if isinstance(instance, BaseTreeNode) and instance.can_have_children:
        for child in instance.children.all():
            child.delete()

This is my BaseTreeNode Model:

class BaseTreeNode(PolymorphicMPTTModel):
    parent = PolymorphicTreeForeignKey('self', blank=True, null=True, related_name='children', verbose_name=_('parent'))

This is an example child:

class Edition(BaseTreeNode):
    pass

Is there a more elegant way?

@vfigueiro
Copy link

Had exact same issue. Thanks for the workaround @mvdwaeter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants