I am trying to persist an object reference using only ZODB in a FileStorage database. I made a test to analyze its performance, but the object when it is loaded it appears to be broken.
The test consists on:
- create an object in one script and write it to database.
- In another script read that object from the same database and use it there.
zodb1.py
import ZODB
from ZODB.FileStorage import FileStorage
import persistent
import transaction
storage = FileStorage('ODB.fs')
db = ZODB.DB(storage)
connection = db.open()
ODB = connection.root()
print(ODB)
class Instrument(persistent.Persistent):
def __init__(self, name, address):
self.name = name
self.address = address
def __str__(self):
return f'Instrument - {self.name}, ID: {self.address}'
camera = Instrument(name='Logitech', address='CAM0')
ODB['camera'] = camera
ODB._p_changed = True
transaction.commit()
print(ODB)
ob = ODB['camera']
print(ob)
print(dir(ob))
zodb2.py
import ZODB, ZODB.FileStorage
import persistent
import transaction
connection = ZODB.connection('ODB.fs')
ODB = connection.root()
print(ODB)
ob = ODB['camera']
print(ob)
print(dir(ob))
Am I missing something important? I've read the ZODB's documentation and I see no other configuration process or another way to aproach this.
Thank you in advance.
I think that the problem you see is because
zodb2.pyhas no knowledge of theInstrumentclass defined inzodb1.py.I guess that if you moved your class to a separate module and imported it in both
zodb1andzodb2, you would not see a broken object.