1
创建它的@classmethod中的实体更新不可靠地保存在数据存储中。@classmethod是否可以修改它在GAE中创建的记录?
我的创建方法如下。该参数是要被持久保存的对象。
@classmethod
def create(cls, obj):
"""Factory method for a new system entity using an System instance. Returns a System object (representation) including the meta_key."""
if isinstance(obj, System):
pass
else:
raise Exception('Object is not of type System.')
#check for duplicates
q = dbSystem.all(keys_only=True)
q.filter('meta_guid = ', obj.meta_guid)
if q.get(): #match exists already
raise Exception('dbSystem with this meta_guid already exists. Cannot create.')
# store stub so we can get the key
act = cls(
meta_status = obj.meta_status,
meta_type = obj.meta_type,
meta_guid = obj.meta_guid,
json = None,
lastupdated=datetime.datetime.now())
act.put()
# get the key for the datastore entity and add it to the representation
newkey = str(act.key())
# update our representation
obj.meta_key = newkey
# store the representation
act.json = jsonpickle.encode(obj)
act.put()
return(obj) #return the representation
我的单元测试的测试证实返回的对象有一个meta_key,并为相关实体的JSON是不是没有:
self.assertIsNotNone(systemmodel.dbSystem().get(s.meta_key).json) #json is not empty
然而,在开发服务器上运行我的应用程序的时候,我发现当稍后检索到此实体时,json字段将间歇性地为NULL。
我花了一些时间研究数据存储模型,试图找到可以解释不一致结果的东西,但没有运气。两个关键的来源是model class以及我在Google代码中找到的非常好的overview of the App Engine datastore。
任何人都可以确认更新创建它的@classmethod中的实体应该被认为是可靠的吗?有没有更好的方法来坚持对象的表示?
Kekito - 感谢您的回应。在该方法中的那一点,我还不知道关键,因为该对象尚未创建。无论如何,重复的检查似乎工作正常。看起来问题在于第二个'act.put()';它并不总是保存实体... – jschnurr 2014-10-04 22:30:15