jbm*_*sso 7 python google-app-engine entity google-cloud-datastore
使用Google App Engine SDK和Python,我遇到了一个问题:我无法访问给定实体属性的ID属性.我可以访问的唯一属性是我的类Model中定义的属性,以及key属性(请参阅下面的答案):
class Question(db.Model):
text = db.StringProperty()
answers = db.StringListProperty()
user = db.UserProperty()
datetime = db.DateTimeProperty()
Run Code Online (Sandbox Code Playgroud)
我可以很好地访问文本,答案,用户,日期时间和关键属性.但是,我无法访问ID属性.例如,在获取所有实体之后(使用Question.all()):
# OK Within a template, this will return a string :
{{ question.text }}
# OK, this will return the entity key :
{{ question.key }}
# KO this will return nothing :
{{ question.id }}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗 ?谢谢 !
我刚刚发现了一个可能的(不优雅的,IMO)解决方案.查询并获取实体后,遍历所有实体并手动添加id参数:
query = Question.all()
questions = query.fetch(10)
# Add ID property :
for question in questions:
question.id = str(question.key().id())
Run Code Online (Sandbox Code Playgroud)
我不认为它是高效的CPU,但它可以作为快速/脏的修复.
| 归档时间: |
|
| 查看次数: |
1805 次 |
| 最近记录: |