[Zope] Save references in a list as object attribut

Vladislav Vorobiev vavvav at mykniga.de
Mon Feb 1 09:54:33 EST 2010


Tres Seaver wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Vladislav Vorobiev wrote:
>   
>> Thierry Florac wrote:
>>     
>>> Le vendredi 29 janvier 2010,
>>>   Vladislav Vorobiev <vavvav at mykniga.de> a écrit :
>>> ======================================================================
>>> ...
>>>
>>>   
>>>       
>>>> Thierry thank you  for answer but it doesn't help.
>>>>
>>>> I implement all what you sad. I tried with normal “list” and than 
>>>> swiched to “PersistentList”
>>>>
>>>> code looks like that:
>>>>
>>>> from persistent.list import PersistentList
>>>> import transaction
>>>>
>>>> <snip>
>>>>
>>>> myList=PersistentList([self.context.pfad, 'object])           
>>>> myList[0] = PersistentList([self.context.pfad, 'object])[0]  #hope I 
>>>> anderstud you right
>>>>
>>>> ob.refList= myList 
>>>> ob._p_changed = True
>>>> ob.refList._p_changed = True 
>>>> self.context._setObject(id, ob)
>>>>
>>>> After add I call again:
>>>>
>>>> ob._p_changed = True
>>>> ob.refList._p_changed = True 
>>>> transaction.commit()
>>>>
>>>> I see the commitet trunsactions in ZMI
>>>>
>>>> The same problem. After restart ist the Attribut not in context.
>>>>
>>>> For example return's
>>>>
>>>> Before restart: ***1
>>>> ob.refList[0].objectValues()[0].absolute_url()
>>>> /pfad/object/FirstObjectOfReferencedObject
>>>>
>>>> After restart only the id of the object:
>>>> ob.refList[0].objectValues()[0].absolute_url()
>>>> FirstObjectOfReferencedObject
>>>>
>>>> Here is a place for an other question:
>>>>
>>>> Normaly  self.absolute_url() returns url with hostname,  
>>>> (http://localhost/bla/bla)
>>>> but already befor restart I get without http://localhost/... see ***1
>>>>
>>>> It seem's that I forgot something.  I would be glad to if somebody 
>>>> explain me this problem.
>>>>     
>>>>         
>>> ======================================================================
>>>
>>> In fact I'm not really sure to understand your data structure and it's
>>> goals...
>>> Could you explain me what you want to do ??
>>>
>>> Thierry
>>>   
>>>       
>> I want to set some references from object to another object through. I try to 
>> explain this with example classes.
>>
>> Example with two classes:
>>
>> #Create classes
>>  >>>class School:
>>  >>>   pass
>>
>>  >>>class People:
>>  >>>   pass
>>
>> #Create objects
>>  >>>p=People()
>>
>>  >>>s0=School()
>>  >>>s0.name="Konrad School"
>>
>>  >>>s1=School
>>  >>>s1.name="Leopold School"
>>
>> Now I set the referece
>>
>>  >>>p.school1=s0
>>  >>>p.school2=s1
>>
>> Now I can access through the attribut to s0 and s1 objects:
>>
>> p.school1.name
>> “Konrad School“
>>
>> This construction works persistent without problems.
>>
>> The idea is to put the references in a list.
>>
>>  >>>p.schools=[s0,s1]
>>
>> to access them
>>
>>  >>>p.schools[0].name
>> “Konrad School“
>>
>>  >>>s1.name="Leo School"
>>  >>>p.schools[1].name
>> “Leo School“
>>
>>  >>>type(p.schools[1])
>> <type 'classobj'>
>>
>> It works in zope without restart. If the server was restartet I get some 
>> unwanted results. See my previews posts.
>> The same effects I get with PersistentLists...
>>
>> I hope it is more  understandable.
>>     
>
> You can't expect to use a simple list as an attribute of a persistent
> object without taking special precautions.  Either you have methods
> which mutate the list, and then set the '_p_changed' attribute to 1:
>
>   def addSchool(self, school):
>       self.schools.append(school)
>       self._p_changed = 1
>
> or re-assign the list attribute:
>
>   def addSchool(self, school):
>       schools = self.schools
>       schools.append(school)
>       self.schools = schools
>
> You shoule be able to use a PersistentList instead of a "plain" Python
> list to avoid the need for one of those two problems:
>
>    from persistent.list import PersistentList
>    ...
>    class Person:
>        def __init__(self):
>            self.schools = PersistentList()
>
>   
Thank you for resonse but it doesn't help.

I post my implementation with results.

#my method
def addFotoalbom(self):
  #test with PersistentList
  self.ref_=PersistentList()
  nb=getattr(self.BoxingCategoryContainer, 'Kat 1').FOTOCAT
       self.ref_.append(nb) 
  #test without List          
self.ref=getattr(self.BoxingCategoryContainer, 'Kat 1').FOTOCAT
  return 'ok'



Results without restarting the server

1.
#absolue_url() method of of context
context.absolute_url()
'http://localhost/CategoryContainer/Kat%201/x1'

x1 is my self-object.

2.
#accessing attribute through the list
list(context.ref_[0].objectValues())[0].absolute_url()
'/testpath/CategoryContainer/Kat%201/FOTOCAT/asa'

'asa' is the first object in FOTOCAT but where is http://localhost
and why is the URL without x1/FOTOCAT?

3.
#accessing attribute without list
context.ref.objectValues()[0].absolute_url()
'http://localhost/CategoryContainer/Kat%201/x1/FOTOCAT/asa'
Thats ok.


After RESTART the Server


1.
context.absolute_url()
'http://boxapp.localhost/BoxingCategoryContainer/Kat%201/x1'

2.
list(context.ref_[0].objectValues())[0].absolute_url()
'asa'
What happaning here?

3.
#accessing attribute without list
context.ref.objectValues()[1].absolute_url()
'http://localhost/CategoryContainer/Kat%201/x1/FOTOCAT/asa'
ok


You see attributes without PersistentList survives the restart, 
PersistentList not.
Why I get some results without "http://localhost" for 
context.absolute_url() befor restart? Its 
'/testpath/CategoryContainer/Kat%201/FOTOCAT/asa' must be
'http://localhost/CategoryContainer/Kat%201/FOTOCAT/asa'.

Any Ideas?
Thanks

Vlad


More information about the Zope mailing list