【发布时间】:2011-09-06 21:58:23
【问题描述】:
我正在用 Python(GAE) 做我的第一个小项目,使用查询操作 GAE 的数据库没有任何困难。但是在编辑单个实体时,我面临一个问题。 我所需要的只是一个简单的计数器,它会在每次站点访问时递增。 所以我创建了一个实体(这完成了一次,只是为了创建实体,然后从项目中删除这段代码):
counter_name = 'default_counter'
def counter_key(counter_n=None):
return db.Key.from_path('Counter', counter_name)
class Counter(db.Model):
amount = db.IntegerProperty()
class CounterClass(webapp.RequestHandler):
def get(self):
counter = Counter(counter_key(counter_name))
counter.amount = 0
counter.put()
没关系。 但是当我尝试增加它时,使用:
counter = db.get(db.Key.from_path('Counter', 'default_counter'))
counter.amount += 1
counter.put()
我收到此错误。
错误 2011-09-06 21:49:41,562 _webapp25.py:464] 'NoneType' 对象 没有属性“数量”回溯(最近一次调用最后一次):文件 "C:\程序文件 (x86)\Google\google_appengine\google\appengine\ext\webapp_webapp25.py", 第 703 行,在 调用 handler.post(*groups) 文件 “H:\gae-bin\counter.py”,行 48、在岗 counter.amount += 1 AttributeError: 'NoneType' 对象没有 属性“金额”
我检查了不同的变体,但仍然无法更改实体的值。 我做错了什么?
提前致谢。
【问题讨论】:
标签: python google-app-engine google-cloud-datastore