简体   繁体   中英

primary key in google appengine datastore

    from google.appengine.ext import db
    from datetime import date    
    class Test(db.Model):
        title=db.StringProperty(required=True)
        tags=db.StringListProperty(required=True)

a print on the Test type of object shows

Test(key_id=1, title='ashu_saved', tags=['db'])

but the key_id attribute is is not accesible by title.key_id .also test.pk returns u'agRibG9nchILEgxhc2lzYWlkX3Rlc3QYAQw' is there a way to obtain nicely looking integer primary keys that i can use in the urls, from the model objects in google-appengine?

Try with:

yourkey = test.key().id()

and to get your value back:

Test.get_by_id(ids = yourkey)

It is invalid solution since all record have id() the best is this:

key = entity.key()
Model.get(key)

Record can have id() or name() but must have key().

Even all your record has id() it is still wrong since entity is addressed by ApplicationId, NamespaceId, ParentModel, Model and than id or key. PRIMARY key is all that fields.

It mean you can have many entities with same id() if you use namespaces or use parent to keep transactions consistent - try avoid id() use if you are not sure about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM