gpt4 book ai didi

python - 为什么 Google App Engine NDB 在 ndb.Model 的 @classmethod 内部时返回 StringProperty(...) 作为属性值?

转载 作者:太空宇宙 更新时间:2023-11-04 10:53:47 26 4
gpt4 key购买 nike

我有一个简单的 NDB 模型:

from google.appengine.ext import ndb
import logging
from libs import Api

class User(ndb.Model):
oauth_token = ndb.StringProperty(required=False, indexed=False)

@classmethod
def update_data(self):
logging.info("uid = %s, oauth token = %s" % (self.key, self.oauth_token))
api = Api.Init(self.oauth_token)

在单独的文件 test.py 中,我有以下代码:

from models import User
from google.appengine.ext import webapp, ndb
import logging

class Test(webapp.RequestHandler):
def get(self):
uid = 1
u = ndb.Key(User, str(uid)).get()
logging.info("uid = %s, oauth token = %s" % (u.key.string_id(), u.oauth_token))
u.update_data()

这是日志中的输出:

INFO     2012-07-22 16:32:42,532 test.py:8] uid = Key('User', '1'), oauth token = xxxxxxxxxxxxxxxxxxx
INFO 2012-07-22 16:32:42,532 user.py:9] uid = ModelKey('__key__'), oath token = StringProperty('oauth_token', indexed=False)

为什么我得到的是 StringProperty('oauth_token', indexed=False) 而不是实际属性的值?

最佳答案

update_data@classmethod修饰,表示update_data中的self是对User的引用类,而不是实例方法。只需删除 @classmethod

关于python - 为什么 Google App Engine NDB 在 ndb.Model 的 @classmethod 内部时返回 StringProperty(...) 作为属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com