How do I override delete() on a model and have it still work with related deletes

Each Answer to this Q is separated by one/two green lines.

I’m having a problem because I’m deleting a Widget by using some_widget_instance.delete(). I also have a model called WidgetFile with an override delete() method so that I can delete files off my hard drive when a WidgetFile is deleted. The problem I’m having is that if I delete a Widget, and it has WidgetFiles related to it like this:

class WidgetFile(models.Model):

    widget = models.ForeignKey(Widget)

Well, when I delete that Widget, it’s WidgetFiles are deleted but the delete() method doesn’t trigger and do my extra hard drive stuff. Any help is much appreciated.

I’m doing the same thing and noticed a nugget in the Django docs that you should think about.

Overriding predefined model methods

Overriding Delete
Note that the delete() method for an object is not necessarily called when deleting objects in bulk using a QuerySet. To ensure customized delete logic gets executed, you can use pre_delete and/or post_delete signals.

This means your snippet will not always do what you want. Using Signals is a better option for dealing with deletions.

I went with the following:

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

@receiver(pre_delete)
def delete_repo(sender, instance, **kwargs):
    if sender == Set:
        shutil.rmtree(instance.repo)

I figured it out. I just put this on that Widget model:

def delete(self):
    files = WidgetFile.objects.filter(widget=self)
    if files:
        for file in files:
            file.delete()
    super(Widget, self).delete()

This triggered the necessary delete() method on each of the related objects, thus triggering my custom file deleting code. It’s more database expensive yes, but when you’re trying to delete files on a hard drive anyway, it’s not such a big expense to hit the db a few extra times.

Using clear() prior to deleting, removes all objects from the related object set.

see django-following-relationships-backward

example:

group.link_set.clear() 
group.delete() 

This seems only be sense-full if one Widget is connected to one WidgetFile exactly. In that case you should use a OneToOneField

from On-to-one examples:

# Delete the restaurant; the waiter should also be removed
>>> r = Restaurant.objects.get(pk=1)
>>> r.delete()

Just to throw in a possible way around this problem: pre-delete signal. (Not in any way implying there’s no actual solution.)

It should look like described on the django site:

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()
    def save(self, *args, **kwargs):
        do_something()
        super(Blog, self).save(*args, **kwargs) # Call the "real" save() method.
        do_something_else()

http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-predefined-model-methods

you forgot to pass some arguments

Is some_widget_instance and instance of Widget or of WidgetFile ? Because if it is an instance of Widget it won’t get your custom delete() function, which is in the WidgetFile class.

From Django 1.9, if You would just define on_delete=models.CASCADE for field, it will remove all related objects on delete.


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .