AttributeError:“module”对象没有“get”或“u insert”属性

2024-06-10 19:08:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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)

Tags: orimportselfjsontrueget电子邮件email