[ZODB-Dev] Why does this useage of __setstate__ fail?
Mika, David P (GE, Research)
mika at crd.ge.com
Thu Jan 17 14:31:27 EST 2008
Thanks David,
Yes, that does work. I am trying to keep the persistence machinery in
the persistent classes themselves. Notice with the subsequent statement
newuser.setColor('red')
----------------------------------------
class User(Persistent):
...
def setColor(self, color):
self.color.setColor(color)
#self._p_changed = True
transaction.commit()
the _p_changed should be set (implicitly), and then all changes fixed
with the transaction.commit() in that method--this doesn't seem to be
the case. I can uncomment the _p_changed = True line in the setColor
method and get the desired result, but I don't see why this is required,
after all, the color attribute is a Persistent class in itself. I
suppose because the Persistent class was added in the __setstate__ it
has not fully been bootstrapped at this point and needs a little help.
(the _p_changed=True is not needed when color is a bonafide attribute)
One final observation: if I instead put the self._p_changed = True
statement in the setColor method of the Color class, it doesn't produce
the desired persistency. This is somehow related to the bootstrapping
mystery.
Thanks,
Dave
________________________________
From: David Binger [mailto:dbinger at mems-exchange.org]
Sent: Thursday, January 17, 2008 1:23 PM
You need to saved the user object with the color attribute.
The __setstate__ is just giving you the unsaved one every time.
Add 'newuser._p_changed = 1' after the first
assert statement below and it will be saved along with
your change to the Color instance.
On Jan 17, 2008, at 11:21 AM, Mika, David P (GE, Research) wrote:
def test_2_setstate(self):
print 'in test2'
newuser = self.userdb[self.id]
# setstate is called subsequently
assert hasattr(newuser, 'color')
assert newuser.getColor() == 'blue'
newuser.setColor('red')
assert newuser.getColor() == 'red'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/zodb-dev/attachments/20080117/0aa649b5/attachment.htm
More information about the ZODB-Dev
mailing list