使用 NDB 时,看起来临时变量也会被缓存。
class MyModel(ndb.Model)
def __init__(self, *args, **kwds):
self._temporary = []
ndb.Model.__init__(self, *args, **kwds)
有没有办法不在缓存中存储临时/辅助变量?
编辑:
这是一个证明我的问题的简单测试用例:
#Python imports
import unittest
# GAE imports
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_util
from google.appengine.ext import ndb
class TestModel(ndb.Model):
username = ndb.StringProperty()
def __init__(self, *args, **kwds):
self._temp = []
ndb.Model.__init__(self, *args, **kwds)
class TestModels(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_taskqueue_stub()
self.testbed.init_urlfetch_stub()
self.testbed.init_mail_stub()
self.testbed.init_images_stub()
self.testbed.init_blobstore_stub()
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
self.testbed.init_memcache_stub()
self.testbed.setup_env(app_id='ndb-test')
def tearDown(self):
self.testbed.deactivate()
def test1(self):
test_model = TestModel()
test_model.username = 'Tom'
test_model._temp = 'Temporary'
test_model.put()
test = TestModel.query().fetch(10)
self.assertEqual(1, len(test))
test = test[0]
self.assertEqual('Tom', test.username)
self.assertEqual([], test._temp)
测试self.assertEqual([], test._temp)的最后一行失败,即使我希望它通过。
有没有办法不存储临时/辅助模型属性,或者每次从缓存中加载模型时都有一种更简单的方法来重置它们?