Python NDB-如何在数据存储忽略的模型实例上存储属性?
我试图在Python NDB-如何在数据存储忽略的模型实例上存储属性?,python,google-app-engine,app-engine-ndb,Python,Google App Engine,App Engine Ndb,我试图在ndb.model类的模型实例上存储属性,以便在写入数据存储时忽略它们,但正确地进行pickle,以便可以存储在Memcache中 例如,我有以下模型:- from google.appengine.ext import ndb class TestModel(ndb.Model): lat = ndb.FloatProperty() lon = ndb.FloatProperty() def set_lat(self, lat): self.
ndb.model
类的模型实例上存储属性,以便在写入数据存储时忽略它们,但正确地进行pickle,以便可以存储在Memcache中
例如,我有以下模型:-
from google.appengine.ext import ndb
class TestModel(ndb.Model):
lat = ndb.FloatProperty()
lon = ndb.FloatProperty()
def set_lat(self, lat):
self.lat = lat
def set_lon(self, lon):
self.lon = lon
def set_child(self, testModel):
setattr(self, 'child', testModel)
我希望我可以将它存储在Memcache中,当我检索它时,所有的成员变量都将被恢复。但是孩子们正在迷路。有关测试,请参阅以下代码
# create a model and assign some props
testModel = test_model.TestModel()
testModel.set_lat(55.55)
testModel.set_lon(-0.565)
testModel.set_child(test_model.TestModel())
# store to memcache
key = "test_model_key"
memcache.set(key, testModel, 0)
# get from memcache
fromCacheModel = memcache.get(key)
# here fromCacheModel.child does not exist??
从Memcache检索的模型没有子属性
这适用于db.Model
,但不适用于ndb.Model
。我想做这件事的原因是我能
将完全形成的对象存储在Memcache中,以便更快地检索(减少rpc调用)。您可以使用expando模型。顺便说一下。我记得过去我使用了一个没有u的属性名,它不是模型的一部分,可以用于此目的。但是……以这种方式重写init将导致问题,除非您了解ndb元类的工作方式(至少尝试调用super)。尝试使用不带前导的名称,只在方法中使用setattr来设置属性值,而不是尝试覆盖uuuuuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。也许有更好的方法可以做到这一点。蒂姆,是的,用这种方法重写init会在nbd.Model中失败。我正在展示我过去在db.Model中所做的工作,并询问如何将其转换为ndb.Model