get().delete() does not work, filter().delete() works?

1,792 views
Skip to first unread message

Rainy

unread,
Mar 16, 2012, 2:33:57 PM3/16/12
to Django users
Hi, I have a function that accepts a pk and deletes an object:

Item.objects.filter(pk=pk).delete()

If I change it to:

Item.objects.get(pk=pk).delete()

it no longer works, without any errors. I tried it using exactly the
same object. I looked at django docs for deletion, the only difference
they mention is that with .filter(), it will also do bulk deletion.

What is the reason for this behaviour? Thanks!

Tom Evans

unread,
Mar 16, 2012, 2:50:25 PM3/16/12
to django...@googlegroups.com

Those are two very different methods, instance.delete() on a model
calls the instance's delete method, which (eventually) via the
Model.delete() method, sends a pre_delete signal, issues a SQL
statement to delete that row from the database and finally issues a
post_delete signal.

queryset.delete() does not do any of that, it simply constructs a
"DELETE FROM ... WHERE ..." query and executes it.

So why does your instance.delete() not work? Top contenders:

1) You have overridden the delete() method, and you don't call the
super class method
2) You have a pre_delete signal that refuses the deletion (altho I
think you would receive an exception in this case?)
3) Something I haven't considered \o/

Cheers

Tom

Rainy

unread,
Mar 16, 2012, 3:35:50 PM3/16/12
to django...@googlegroups.com


On Friday, March 16, 2012 2:50:25 PM UTC-4, Tom Evans wrote:

1) You have overridden the delete() method, and you don't call the

super class method


Indeed, I had overridden it to create a delete column and forgot about it!
Thanks.  Rainy
 

Reply all
Reply to author
Forward
0 new messages