On Fri, Mar 16, 2012 at 6:33 PM, Rainy <andrei....@gmail.com> wrote:
> 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!
>

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to