[Grok-dev] model not getting persisted

Kevin Teague kevin at bud.ca
Thu Mar 22 19:49:51 EDT 2007


The first case assigns x as a class attribute. The second case  
assigns x as an instance attribute. The ZODB only persists instance  
attributes - as far as I know?

class attributes are bound to the class itself, and not a particular  
instance.

 >>> class Foo(object):
...   x = ['cat']
...
 >>> f = Foo()
 >>> f.x.append('dog')
 >>> g = Foo()
 >>> g.x
['cat', 'dog']
 >>> g.x = ['meow','bark']
 >>> g.x
['meow', 'bark']
 >>> g.__class__.x
['cat', 'dog']
 >>> f.x
['cat', 'dog']

Personally, I do not use class attributes as defaults for instance  
attributes, but YMMV.

On Mar 22, 2007, at 3:24 PM, Tim Terlegård wrote:

> If I have a model like this the x variable won't survive a server
> restart.
>
> class MyModel(grok.Model):
>     x = PersistentList()
>
> After I restart server the list is empty again. If the model is like
> this it's ok.
>
> class MyModel(grok.Model):
>     def __init__(self):
>         self.x = PersistentList()
>
> I think I can do both ways in pure zope3, but when doing the former  
> way
> in grok it won't be persistent. How come?
>
> Tim
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev
>



More information about the Grok-dev mailing list