使用 GAE 数据存储和命名空间时出现类型错误 - 无法检索实体

发布于 2024-11-02 23:01:20 字数 822 浏览 0 评论 0原文

我正在尝试检索使用数据存储区存储的一些 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

这是我使用数据查看器获得的内容:

credentials are block out

关于我的任何想法做错了吗?

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:

credentials are blocked out

Any ideas as to what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2024-11-09 23:01:20

您正在重写数据存储模型类上的 __init__ 方法,并且您没有将关键字参数传递给父构造函数(或根本不调用它!)。

作为一般规则,您不应重写数据存储模型类的构造函数。可以正确地做到这一点,但这很棘手,而且提供一个类方法作为工厂要安全得多,如下所示:

class OAuthConsumer(db.Model):
    '''the oauth consumer information'''
    consumer_key = db.StringProperty()
    consumer_secret = db.StringProperty()

    @classmethod
    def new(cls, service):
        namespace_manager.set_namespace(service)
        query = db.GqlQuery('SELECT * FROM OAuthConsumer')
        creds = query.get()
        return cls(consumer_key=creds.consumer_key, consumer_secret=creds.consumer_secret)

creds = OAuthConsumer('google')

不过,由于以下几个原因,您的代码有点奇怪:

  • 在构造新实例时,您从同一模型的另一个(有效)随机选择的实例中获取并复制字段!
  • 您正在构造函数内(或在重写版本中,在工厂方法中)设置名称空间。命名空间是一个全局设置,您确实不应该在库方法内执行此操作。之后您也不会将其设置回来。

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:

class OAuthConsumer(db.Model):
    '''the oauth consumer information'''
    consumer_key = db.StringProperty()
    consumer_secret = db.StringProperty()

    @classmethod
    def new(cls, service):
        namespace_manager.set_namespace(service)
        query = db.GqlQuery('SELECT * FROM OAuthConsumer')
        creds = query.get()
        return cls(consumer_key=creds.consumer_key, consumer_secret=creds.consumer_secret)

creds = OAuthConsumer('google')

Your code is more than a little odd for a couple of reasons, though:

  • When constructing a new instance, you fetch and copy the fields from another, (effectively) randomly selected instance of the same model!
  • You're setting the namespace inside a constructor (or in the rewritten version, in a factory method). The namespace is a global setting, and you really shouldn't do this inside a library method. You don't set it back afterwards, either.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文