Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete() an instance

In my view:

def delete_payment(request, id):
    thePayment = Payment.objects.filter(id=id)
    thePayment.delete()
    return HttpResponseRedirect('/invoices/open/')

In my model:

def delete(self, *args, **kwargs):
    raise Exception('foo')
    super(Payment, self).delete(*args, **kwargs)

I'm finding that the exception doesn't get raised unless I delete the instance from within the admin view. That is, I can't get delete() to be called properly if I use my own view.

like image 658
Harv Avatar asked Mar 16 '26 06:03

Harv


1 Answers

Manager.filter() returns a QuerySet, not a Model. QuerySet.delete() doesn't invoke Model.delete() but rather operates directly on the database.

like image 128
Ignacio Vazquez-Abrams Avatar answered Mar 17 '26 21:03

Ignacio Vazquez-Abrams