[Grok-dev] Pickling an object stored in the Zodb. How do I skip the __parent__ attribute?
sebastian at urbantalk.se
Fri Aug 15 10:19:25 EDT 2008
I have succeeded in exporting a list of objects using an adapter to
override __getstate__ for the root object. I have no references bellow
the "root objects" (except __parent__ in each root object) so I don't
have to worry about checking if objects are "outside" the branch that
I am exporting. The code is basically the following:
This adapter is used for the root object that I want to export.
removes the __parent__ reference from the __getstate__ dictionary
thus stopping the the pickler from traversing bellow the root
in the object graph.
state = self.context.__getstate__()
and it is called from an export view that looks like this:
tmp = 
for obj in self.context['default'].values():
xobj = interfaces.IExportableObject(obj)
self.exportdata = cPickle.dumps(tmp)
from tempfile import TemporaryFile
exportfile = TemporaryFile('w')
filename = self.context.__name__ + '.export'
response = self.request.response
'attachment; filename=%s' % filename)
This exports the data including annotations. Now I am going to start
working on the import :)
14 aug 2008 kl. 20.03 skrev Kevin Smith:
> Hi Sebastion,
> Please let me know if you make headway with this, I've tried a
> number of things to make this work, without success.
> Kevin Smith
> Sebastian Ware wrote:
>> Thanks for the pointer! I have read:
>> And found that the pickling is done by calling the method you refered
>> and that it really does exist on the objects in the Zodb. I will
>> override the method by means of an adapter for the root object that I
>> am pickling and remove the attribute [__parent__]. Feels as though
>> this might actually work :)
>> Mvh Sebastian
>> 14 aug 2008 kl. 15.11 skrev Jegenye 2001 Bt (Miklós Prisznyák):
>>> 2008/8/14 Sebastian Ware <sebastian at urbantalk.se>
>>> I am trying to export and import objects from the Zodb using
>>> My problem is that the pickler traverses the __parent__ attribute
>>> returning a much larger object graph than I wish to serialize.
>>> Do I have to subclass the pickler or is there som other smart way of
>>> making it skip the __parent__ attribute?
>>> You could write an adapter (or modify the class of the content
>>> objects itself) to use __setstate__ and __getstate__ , etc:
>>> Best regards,
>>> Grok-dev mailing list
>>> Grok-dev at zope.org
>> Grok-dev mailing list
>> Grok-dev at zope.org
More information about the Grok-dev