Unable to access ID property from a datastore entity

假装没事ソ 提交于 2019-12-04 06:19:14

According to the documentation, there is no id() instance method defined for Model subclasses.

Try {{ question.key }} instead.

Also note that the key is not created until the entity is saved to the datastore.


Edit: more info based on OP's edit:

Since we're really after the numeric ID, we could do something like this in our template:

{{ question.key.id }}

Another note: you should never expect numeric IDs to increase in value corresponding with the order of entity creation. In practice, this is usually—but not always—the case.

I just found a possible (inelegant, IMO) solution. After querying and fetching entities, loop through all of them and manually add the id parameter :

query = Question.all()
questions = query.fetch(10)

# Add ID property :
for question in questions:
    question.id = str(question.key().id())

I don't think it's efficient CPU wise, but it works as a quick/dirty fix.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!