[Zope] List attribute inside of a product...

Chris McDonough chrism@zope.com
29 May 2002 22:01:24 -0400


You have two bad assumptions here.  They're unrelated except for the
fact that code written with either assumption will exhibit the problem
you noted.

The first is that you cannot use a class attribute to do this because
class attributes are not persistent.  You need to make categoryList an
instance attribute (instead of a class attribute).

The second is that ZODB cannot detect the mutation of basic types and
thus the transaction that results from a web request that calls
manage_addCategory won't notice that anything has changed.  You need to
give it a clue by manually signifying that the instance has changed.

I imagine that DefaultDublinCoreImpl already has an __init__ method
(this is the usual place to initialize instance attributes), so I won't
put an __init__ method in my amende example below.  Instead, we'll set
one dynamically based on the value of the class attribute.  We'll also
set a magic attribute (_p_changed) on the instance to tell ZODB that
something has changed as a result of the method call.

For more info on how to use persistence, see
http://www.zope.org/Documentation/ZDG/Persistence.stx 

For more info on class attributes vs. instance attributes, a good
reference is the book "Learning Python" (O'Reilly).

class Store(Folder.Folder, PortalContent, DefaultDublinCoreImpl):
    """ Store Class """

    meta_type = 'Store'
    _isDiscussable = 1
    icon = PortalContent.icon
    categoryList = None

    def manage_addCategory(self, id, title, REQUEST = None):
        if self.categoryList is None:
            self.categoryList = []
        self.categoryList.append(id)
        self._p_changed = 1

On Wed, 2002-05-29 at 21:35, Jeff Ross wrote:
> I'm writing a product that needs a list of items inside it, e.g ['abc',
> '456', '243'].
> 
> The list stays intact if the server is up. But if I shutdown and restart,
> the list is cleared again.
> 
> Here's some of my code:
> 
> class Store(Folder.Folder, PortalContent, DefaultDublinCoreImpl):
>     """ Store Class """
> 
>     meta_type = 'Store'
>     _isDiscussable = 1
>     icon = PortalContent.icon
>     categoryList = []
> 
>     def manage_addCategory(self, id, title, REQUEST = None):
>         self.categoryList.append(id)
> 
> Is this the reason my list is getting reset to empty on restart? I don't
> understand. If I have a text attribute and set it to empty in the class
> code, then it's fine. I get my saved attribute. But my list is blown away.
> 
> Jeff
> 
> 
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )