【发布时间】:2014-07-20 09:09:52
【问题描述】:
假设我定义了一个新的持久类 Account,如 在the ZODB tutorial。
import persistent, ZODB, ZODB.FileStorage, import transaction
class Account(persistent.Persistent):
def __init__(self):
self.balance = 0.0
def deposit(self, amount):
self.balance += amount
def cash(self, amount):
assert amount < self.balance
self.balance -= amount
storage = ZODB.FileStorage.FileStorage('mydata.fs')
db = ZODB.DB(storage)
connection = db.open()
root = connection.root
root.accounts = BTrees.OOBTree.BTree()
root.accounts['account-1'] = Account()
transaction.commit()
运行此脚本后,我编写了另一个脚本来访问该对象 我创建的。
import persistent, ZODB, ZODB.FileStorage
class Account(persistent.Persistent):
...
storage = ZODB.FileStorage.FileStorage('mydata.fs')
db = ZODB.DB(storage)
connection = db.open()
root = connection.root
print(root.accounts['account-1'])
print(root.accounts['account-1'].balance)
第二个脚本的输出是:
<__main__.Account object at 0x95fbaac>
0.0
但是,如果我在定义类 Account 的行中添加注释,则输出为:
<persistent broken __main__.Account instance '\x00\x00\x00\x00\x00\x00\x00\x02'>
Traceback (most recent call last):
File "test.py", line 24, in <module>
print(root.accounts['account-1'].balance)
AttributeError: 'Account' object has no attribute 'balance'
我认为对象声明没有附加到数据库,所以 我们无法执行我之前在 Account 中定义的方法。但 我不清楚我是否可以访问对象中的属性 没有找到类定义。所以我的问题是:有没有基础 可用于访问没有类的对象的数据模型 ZODB 中的定义?
我的问题也是出于以下担忧:
我习惯于数据库具有数据模型的应用程序 与应用程序逻辑分离。假设我合并了一个新的 应用程序中的模块 X(定义类),然后我发现 X 在我的应用程序中产生了问题。在经典方法中,我 总是可以停止应用程序并检查数据库,而无需 逻辑层来分析并尝试修复数据。然后,启动 在没有模块 X 的情况下再次应用程序。但在 ZODB 中我没有找到 关于可以在没有类的情况下使用的数据层的文档 定义。
【问题讨论】: