2017-01-26 13 views
0

Googleのデータストアに簡単なPythonアプリケーションを作成しようとしていますが、重複したメールを保存せずにニュースレターのメールを保存しています。 1.どのようにエラーを修正しますか? 2.電子メールが存在し、「購読済み」=偽の場合、それをどのようにして真に更新しますか?AttributeError: 'module'オブジェクトに 'get_or_insert'属性がありません

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) 

答えて

0

get_or_insertndb.Modelクラスのではなく、ndbモジュールの方法である、Class Methodsを参照。

だからあなたが使用することをお勧めします

entity = ndb.Model.get_or_insert(ekey) 

あるいは

entity = Email.get_or_insert(ekey) 
関連する問題