Google App Engine - Datastore get_or_insert key_name confusion

后端 未结 2 979
[愿得一人]
[愿得一人] 2021-01-06 04:34

I am confused by get_or_insert(..). What should I pass as the key_name? Consider the following example:

class Person(db.model)
             


        
2条回答
  •  心在旅途
    2021-01-06 04:48

    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")
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题