我在现有的数据存储中有一个模型。看起来像这样:
class SomeKind(db.Model):
name = db.StringProperty(required=True)
someField = db.BlobProperty(required=True)
数据存储中大约有20000多个此类实体 现在我想重组一下,让它变成这样:
^{pr2}$我想我必须
我在(1)处遇到了问题,我想用远程API删除现有属性:
import sys, time, urllib2
sys.path.append("gae/paths")
...
sys.path.append("myapp/path")
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.ext.remote_api import remote_api_stub
from models import *
def tryPut(db, set, tryLimit=10, seconds=5, trying=1):
try:
db.put(set)
return True
except urllib2.HTTPError:
if trying <= tryLimit:
print "retry (%d of %d) in 5 seconds" % (trying, tryLimit)
time.sleep(5)
tryPut(db, set, seconds, trying+1)
else:
print urllib2.HTTPError
sys.exit()
def main():
remote_api_stub.ConfigureRemoteDatastore(None,
'/remote_api', my_auth_func, 'myapp.appspot.com')
q = db.GqlQuery("SELECT * FROM SomeKind")
last_cursor = memcache.get('SomeKind/update')
if last_cursor:
q.with_cursor(last_cursor)
set = q.fetch(100)
while len(set) != 0:
for someKind in set:
print someKind.name
# this doesn't work
delattr(someKind, "someField")
# this doesn't work either
del someKind.someField
print "update to Google"
if tryPut(db, set):
cursor = q.cursor()
memcache.set('SomeKind/update', cursor)
if __name__ == "__main__":
main()
我在我的机器上运行这个。 问题是,无论使用哪种方法,此脚本都会引发错误:
Traceback (most recent call last):
File "./query.py", line 91, in <module>
main()
File "./query.py", line 66, in main
del someKind.someField
AttributeError: __delete__
文档(http://code.google.com/intl/en/appengine/articles/update_schema.html)说了类似“使用delattr删除过时属性,然后保存实体”的内容。但从来没有这样的例子。在
我该怎么做? 我的脚步正确吗? 我是怎么删除属性的?在
不能从模型中删除属性-每个模型实例都有相同的属性集。^然而,{a1}允许您拥有动态属性。在
最简单的方法可能是:
像这样使用
以及
^{pr2}$完整代码在这里-http://appengine4dummies.blogspot.com/2012/01/text-that-matters-deleting-appengine.html
相关问题 更多 >
编程相关推荐