strange problem deleting objects

21 views
Skip to first unread message

Marc Aymerich

unread,
Mar 28, 2012, 7:06:29 PM3/28/12
to django...@googlegroups.com
Hi!,
I've overrided the save() method of one of my ModelForms and I'm
performing a delete of some related objects depending on some form
fields values. The thing is I'm getting differents behaviours
dependening on how the deletion is performed, consider:

1) self.instance.delete()
2) MyModel.objects.get(pk=self.instance.pk).delete()

I'm listening the post_delete of self.instance class, in there I
create a "dependency graph" of their related objects. The problem is
that at some point I'm retriveing self.instance like:
MyRelatedModel.self_instance_related_name and with the first (1)
delete method I can still access to self.instance but with the second
(2) delete method i'm getting a DoesNotExist.

Why it's different?

Thanks!
--
Marc

Daniel Roseman

unread,
Mar 29, 2012, 5:15:50 AM3/29/12
to django...@googlegroups.com
self.instance.delete() will delete the item from the database, but the instance itself will still exist in memory. As long as you don't save it, it will disappear when it goes out of scope, or you can explicitly call del() on it.
--
DR.

Marc Aymerich

unread,
Mar 29, 2012, 5:47:13 AM3/29/12
to django...@googlegroups.com

Hi Daniel,
thanks for your answer!
mmm, but deleting like
MyModel.objects.get(pk=self.instance.pk).delete() self.instance will
continue exist too, isn't it?

Marc Aymerich

unread,
Mar 29, 2012, 10:49:07 AM3/29/12
to django...@googlegroups.com

Maybe it could be something related with db transactions?
--
Marc

Marc Aymerich

unread,
Mar 29, 2012, 5:03:36 PM3/29/12
to django...@googlegroups.com
On Thu, Mar 29, 2012 at 11:47 AM, Marc Aymerich <glic...@gmail.com> wrote:

I give it a second thought and I think that you were absolutely right
Danniel. In (1) the post_save is called with a new object that no
longer exists when post_save is called and with (2) is called with
self.instance that still exists after post_save. Thanks and sorry for
not catching it at the first time.

thanks again :)
--
Marc

Reply all
Reply to author
Forward
0 new messages