views:

40

answers:

1

if i use this :

class A(db.Model):
    a=db.StringProperty()

class demo(BaseRequestHandler):
    def get(self):
        a=A()
        a.a='sss'
        a.put()
        raise Exception(a.key().id())

i can get the a.key().id() is 961

but if i add key_name="aaa" , the a.key().id() will be None :

class A(db.Model):
    a=db.StringProperty()

class demo(BaseRequestHandler):
    def get(self):
        a=A(key_name="aaa")
        a.a='sss'
        a.put()
        raise Exception(a.key().id())

so how can i get the key().id() when i set a key_name

thanks

+4  A: 

You can't, because they're the same thing.

The fact that entities have an encoded string key plus either an integer ID or a string name can give the misleading impression that the various ways to refer to an entity are overlapping or redundant. They're not.

A key name is like a filename within a filesystem. An ID is like a filename that the system has picked automatically. The key itself is like the full path to the file, including directories.

Consider the Key.from_path method:

k = Key.from_path('User', 'Boris', 'Address', 9876)

kind=User&name=Boris is like a directory, and kind=Address&name=9876 is like a file containing your entity. The key returned is just an encoded version that path.

App Engine relies on each entity have one fixed, immutable path, ergo one key. If an entity could be represented by both a user-assigned name and a system-assigned ID, this would mean that a single entity with n ancestors could have 2^(n+1) different paths and keys.

Drew Sears