On Tue, Mar 24, 2009 at 6:10 PM, Thomas Guettler <h...@tbz-pariv.de> wrote:
>
> Hi,
>
> book2.publisher is an attribute which was created before you deleted it.
> If you get book2 again from Book.objects, this attribute would not exist
> anymore.
>
> I have not looked at the underlaying django source, but I guess that
> remove(obj) removes the attribute from obj.
>
> But clear() can't do this, since it does not have a reference to all
> affected objects.

Your guess at the implementation is correct. In the implementation of
remove(), we have a handle to the object that is being removed, so we
are able to update the value of the publisher attribute on the object.
In the case of clear(), we don't have a handle to the object being
updated, so we can't update the publisher attribute.

Two other interesting manifestations/side effects of this behaviour
that are worth note:

 * If you have a second reference to book2, it won't be updated in the
case of remove():

>>> book1 = pub.book_set.create(title="title1")
>>> otherbook1 = Book.objects.get(title="title1")
...
>>> otherbook1.publisher
<Publisher: pub1>
>>> pub.book_set.remove(book1)
>>> book1.publisher
>>> otherbook1.publisher
<Publisher: pub1>

 * If you re-retrieve book2, the publisher attribute will be correct:
>>> pub.book_set.clear()
>>> book2.publisher
<Publisher: pub1>
>>> otherbook2 = Book.objects.get(title="title2")
>>> otherbook2.publisher
# value is none, as expected

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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