Skip to content

Instantly share code, notes, and snippets.

@wojas
Created November 12, 2012 12:40
Show Gist options
  • Save wojas/4059168 to your computer and use it in GitHub Desktop.
Save wojas/4059168 to your computer and use it in GitHub Desktop.
Workaround for FeinCMS bug, where objects cannot be deleted if there are content types linking to it (IntegrityError)
# Workaround for: https://github.com/feincms/feincms/issues/323
# "Unable to delete pages containing custom content types"
from django.db.models.signals import pre_delete
import logging
log = logging.getLogger(__name__)
def fix_content_model_delete(model):
"""Register a pre-delete handler that will remove all content when a
content holder is deleted. This prevents a referential integrity error
when deleting the content holder in MySQL.
:param model: content type holder inheriting from a FeinCMS Base class
:type model: feincms.models.Base
"""
if not hasattr(model, '_feincms_content_types'):
raise TypeError("Not a FeinCMS Base type model: {0!r}".format(model))
def handler(sender, instance, **kwargs):
log.info('pre_delete signal for %r', instance)
for ct in model._feincms_content_types:
log.info('deleting: %r references to %r', ct, instance)
# Delete all content for the instance to be deleted
ct.objects.filter(parent=instance).delete()
# weak=False, because the handler will go out-of-scope
pre_delete.connect(handler, sender=model, weak=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment