简体   繁体   English

谷歌appengine数据存储区中的主键

[英]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? 但是title.key_id不能访问key_id属性。否则test.pk返回u'agRibG9nchILEgxhc2lzYWlkX3Rlc3QYAQw'有没有办法从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: 这是无效的解决方案,因为所有记录都有id(),最好是这样:

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

Record can have id() or name() but must have key(). 记录可以有id()或name()但必须有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. 即使你的所有记录都有id(),它仍然是错误的,因为实体由ApplicationId,NamespaceId,ParentModel,Model以及id或key来寻址。 PRIMARY key is all that fields. PRIMARY键就是所有字段。

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. 这意味着如果您使用命名空间或使用父级来保持事务一致,您可以拥有许多具有相同id()的实体 - 如果您不确定,请尽量避免使用id()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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