Google App Engine - Datastore get_or_insert key_name confusion [英] Google App Engine - Datastore get_or_insert key_name confusion
问题描述
我对 get_or_insert(..)
感到困惑。作为 key_name
,我应该传递什么?考虑下面的例子:
I am confused by get_or_insert(..)
. What should I pass as the key_name
? Consider the following example:
class Person(db.model)
name = db.StringProperty(required=True)
假设没有两个人可以拥有相同的名称。说我想要获得或插入一个名为 Peter
的新人
,我的 get_or_insert
语句看起来像什么?
Assume that no two people can have the same name. Say I want to get or insert a new Person
with name Peter
, what would my get_or_insert
statement look like?
干杯,
Pete
推荐答案
这里可能令人困惑的是,键不直接显示在你的模型声明中,所以 name = db .StringProperty
位不需要。 get_or_insert
将键名作为其第一个(必需)参数,因此您要查找的代码如下:
The possibly confusing thing here is that the key doesn't show up directly in your model declaration, so that name = db.StringProperty
bit in your example isn't needed. get_or_insert
takes the keyname as its first (required) argument, so you're looking for code like:
person = Person.get_or_insert('Peter')
# other interesting & useful code here...
# later, maybe we need to retrieve that key name from the object:
name = person.key().name()
assert(name == "Peter")
这篇关于Google App Engine - Datastore get_or_insert key_name confusion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!