使用 GAE 数据存储和命名空间时出现类型错误 - 无法检索实体
我正在尝试检索使用数据存储区存储的一些 OAuth 信息,但在实例化 OAuthConsumer 类时遇到此错误:
TypeError: __init__() got an unexpected keyword argument 'consumer_secret'
这也是我第一次尝试命名空间,我想知道这是否是这样与此有关。课程如下:
creds = OAuthConsumer('google')
class OAuthConsumer(db.Model):
'''the oauth consumer information'''
consumer_key = db.StringProperty()
consumer_secret = db.StringProperty()
def __init__(self, service):
namespace_manager.set_namespace(service)
query = db.GqlQuery('SELECT * FROM OAuthConsumer')
creds = query.get()
self.consumer_key = creds.consumer_key
self.consumer_secret = creds.consumer_secret
这是我使用数据查看器获得的内容:
关于我的任何想法做错了吗?
I'm trying to retreive some OAuth information that I've stored using Datastore, but I'm getting this error when I'm instantiating my OAuthConsumer class:
TypeError: __init__() got an unexpected keyword argument 'consumer_secret'
This is also my first time experimenting with Namespace, and I'm wondering if that has something to do with it. The class is as follows:
creds = OAuthConsumer('google')
class OAuthConsumer(db.Model):
'''the oauth consumer information'''
consumer_key = db.StringProperty()
consumer_secret = db.StringProperty()
def __init__(self, service):
namespace_manager.set_namespace(service)
query = db.GqlQuery('SELECT * FROM OAuthConsumer')
creds = query.get()
self.consumer_key = creds.consumer_key
self.consumer_secret = creds.consumer_secret
Here's what I've got using Data Viewer:
Any ideas as to what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在重写数据存储模型类上的
__init__
方法,并且您没有将关键字参数传递给父构造函数(或根本不调用它!)。作为一般规则,您不应重写数据存储模型类的构造函数。可以正确地做到这一点,但这很棘手,而且提供一个类方法作为工厂要安全得多,如下所示:
不过,由于以下几个原因,您的代码有点奇怪:
You're overriding the
__init__
method on a Datastore Model class, and you're not passing the keyword arguments through to the parent constructor (or calling it at all!).As a general rule, you shouldn't override the constructor of a Datastore Model class. It's possible to do it right, but it's tricky, and it's far safer to provide a class method as a factory, like this:
Your code is more than a little odd for a couple of reasons, though: