jason
jason

Reputation: 3075

Django multiple delete does not work

I have the following delete method, this works when deleting just 1 record individually but not when deleting multiple ones in the admin i.e. via list view, why and how to fix?

def delete(self, *args, **kwargs):

        this = Profile.objects.get(id=self.id)
        this.image.delete(save=True)

        super(Profile, self).delete(*args, **kwargs)

Upvotes: 0

Views: 151

Answers (2)

Jeremy T
Jeremy T

Reputation: 1293

The Django docs actually mention your exact situation with the admin action: https://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/

Alternatively, you could look into overriding the default Manager and QuerySet models, if you want to use Model.objects.delete elsewhere.

Upvotes: 1

msc
msc

Reputation: 3800

I recommend using signals to delete your related image, check out the pre_delete signal in the documentation.

Upvotes: 2

Related Questions