Thursday, March 29, 2012

Re: strange problem deleting objects

On Thursday, 29 March 2012 00:06:29 UTC+1, Marc Aymerich wrote:
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


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.

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

No comments:

Post a Comment