开发者

KindError in Google App Engine

I defined a simple class in GAE for keeping user profiles data like this:

class User(db.Model):
    email = db.EmailProperty()
    role = db.StringProperty(default=roles.USER)
    first_name = db.StringProperty()
    last_name = db.StringProperty()
...

I use memcache to keep session information. memcache data looks like this { 'key': 'agpjYW5kaXJhdGVzcgoLEgRVc2VyGCMM'}. I get session_id value from the cookie. When I try to get user info linked to that cookie like this:

session_id = request['session_id']
data = memcache.get(session_id)
user = User开发者_开发问答.get(data['key'])

I get KindError exception:

KindError: Kind 'User' is not a subclass of kind 'User'

I know this user exists, memcache exists. User class is defined only once in my project. Why this error occurs and how can I make it work?

UPDATE: I tried to use db.get() instead of User.get() and it worked. So, what's the problem there can be?


Model.get() does check whether the supplied key is of the correct kind, as defined in the documentation. If not of the correct kind it will throw a KindError.

db.get() does not do any type checking and therefore will succeed with the supplied value if it exists in the data store, but will not necessarily return a User entity.

So you need to check whether the key in your memcache is actually of the User kind. Are you sure it's not overwritten with the key of a different model at some point?


The App Engine framework defines a class called 'User' as part of the Users API. In addition, you have your own class by the same name. When the exception occurs, you're trying to use one, but getting the other.

To avoid this, rename your model. You should also be careful how you import modules in Python. Instead of:

from google.appengine.api.users import User

or worse:

from google.appengine.api.users import *

you should use:

from google.appengine.api import users

And then refer to users.User, which is unambiguous.


The problem, it seems to me, is more subtle than that. I was getting the error with this call to Model.get() (I'm retrieving a top-level singleton object, always there):

datastore = GDSDatastore.get(gds.Key.from_path(*path))

so I investigated with this code:

datastore = gds.get(gds.Key.from_path(*path))
if not(datastore is None or isinstance(datastore, GDSDatastore)):
    logger.error("KindError isinstance(GDSDatastore)=%s class=%s" % (isinstance(datastore, GDSDatastore), datastore.__class__.__name__))
    raise gds.KindError('Kind %r is not a GDSDatastore instance' %
                    (datastore.kind()))

The vast majority of the time I get no error, but today I got this interesting log:

KindError isinstance(GDSDatastore)=False class=GDSDatastore

Now, that strikes me as rather peculiar.

(Note: GDSDatastore is defined locally: class GDSDatastore(gds.Model))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜