ojengwa / django-tagging

Automatically exported from code.google.com/p/django-tagging
Other
1 stars 0 forks source link

Delete object tags on pre_delete signal. #162

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It should be usefull to automatically delete all object's tags (TaggedItem
objects) when object is deleted().

To solve this issue now I have to write following code in each model:

    def delete(self):
        # Deleting all asociated tags.
        Tag.objects.update_tags(self, None)
        super(MyModel, self).delete() # Call the "real" delete() method

Of course I can write more generic code, but it would by nice if such logic
will be placed in tagging app.

Original issue reported on code.google.com by n.le...@gmail.com on 17 Sep 2008 at 12:52

GoogleCodeExporter commented 9 years ago

Original comment by jonathan.buchanan on 30 Oct 2008 at 2:44

GoogleCodeExporter commented 9 years ago
I would like this also!

Original comment by phoebebr...@gmail.com on 11 Aug 2009 at 12:47

GoogleCodeExporter commented 9 years ago
Here's the handler to do it for all models. Since it deletes TaggedItems for 
ALL deleted model instances (with an integer primary key, since those are the 
only model types you can tag), it's pretty heavy handed. But you only have to 
write this handler once and connect it once.

from django.contrib.contenttypes.models import ContentType
from django.db.models import signals
from tagging.models import TaggedItem

def taggeditem_delete(sender, **kwargs):
    deleted = kwargs['instance']
    try:
        id = int(deleted.pk)
    except ValueError:
        return
    ctype = ContentType.objects.get_for_model(deleted)
    item_tags = TaggedItem.objects.filter(
            content_type=ctype,
            object_id=id,
        )
    item_tags.delete()

signals.post_delete.connect(taggeditem_delete)

Original comment by john.kur...@gmail.com on 9 Oct 2010 at 1:10