AttributeError: 'module' object has no attribute 'get_or_insert'
AttributeError: 'module' object has no attribute 'get_or_insert'
我正在尝试使用 Google 的数据存储创建一个简单的 python 应用程序,该应用程序存储时事通讯的电子邮件而不存储重复的电子邮件,但我的代码抛出错误 AttributeError: 'module' object has no attribute 'get_or_insert'
1.如何修复错误?
2. 如果电子邮件确实存在,并且 "subscribed"=false,我如何将其更新为 True?
import webapp2
import json
from google.appengine.ext import ndb
class Email(ndb.Model):
subscribed = ndb.BooleanProperty()
@staticmethod
def create(email):
ekey = ndb.Key("Email", email)
entity = ndb.get_or_insert(ekey)
if entity.subscribed: ###
# This email already exists
return None
entity.subscribed = True
entity.put()
return entity
class New(webapp2.RequestHandler):
def post(self):
Email().create(self.request.get('email'))
self.response.headers['Content-Type'] = 'application/json'
obj = {
'success': True
}
self.response.out.write(json.dumps(obj))
app = webapp2.WSGIApplication([
webapp2.Route(r'/parse', New),
], debug=True)
get_or_insert
是 ndb.Model
class 的方法,而不是 ndb
模块的方法,请参阅 Class Methods。
所以你可能想使用
entity = ndb.Model.get_or_insert(ekey)
甚至
entity = Email.get_or_insert(ekey)
我正在尝试使用 Google 的数据存储创建一个简单的 python 应用程序,该应用程序存储时事通讯的电子邮件而不存储重复的电子邮件,但我的代码抛出错误 AttributeError: 'module' object has no attribute 'get_or_insert'
1.如何修复错误?
2. 如果电子邮件确实存在,并且 "subscribed"=false,我如何将其更新为 True?
import webapp2
import json
from google.appengine.ext import ndb
class Email(ndb.Model):
subscribed = ndb.BooleanProperty()
@staticmethod
def create(email):
ekey = ndb.Key("Email", email)
entity = ndb.get_or_insert(ekey)
if entity.subscribed: ###
# This email already exists
return None
entity.subscribed = True
entity.put()
return entity
class New(webapp2.RequestHandler):
def post(self):
Email().create(self.request.get('email'))
self.response.headers['Content-Type'] = 'application/json'
obj = {
'success': True
}
self.response.out.write(json.dumps(obj))
app = webapp2.WSGIApplication([
webapp2.Route(r'/parse', New),
], debug=True)
get_or_insert
是 ndb.Model
class 的方法,而不是 ndb
模块的方法,请参阅 Class Methods。
所以你可能想使用
entity = ndb.Model.get_or_insert(ekey)
甚至
entity = Email.get_or_insert(ekey)