私の質問は、新しいモデル エンティティを作成し、その直後にそれを読み取るための最良の方法は何かということです。例えば、
class LeftModel(ndb.Model):
name = ndb.StringProperty(default = "John")
date = ndb.DateTimeProperty(auto_now_add=True)
class RightModel(ndb.Model):
left_model = ndb.KeyProperty(kind=LeftModel)
interesting_fact = ndb.StringProperty(default = "Nothing")
def do_this(self):
# Create a new model entity
new_left = LeftModel()
new_left.name = "George"
new_left.put()
# Retrieve the entity just created
current_left = LeftModel.query().filter(LeftModel.name == "George").get()
# Create a new entity which references the entity just created and retrieved
new_right = RightModel()
new_right.left_model = current_left.key
new_right.interesting_fact = "Something"
new_right.put()
これにより、次のような例外がスローされることがよくあります。
AttributeError: 'NoneType' object has no attribute 'key'
つまり、新しい LeftModel エンティティの取得に失敗しました。私は appengine でこの問題に数回直面しましたが、私の解決策は常に少しハッキーでした。通常、エンティティが正常に取得されるまで、すべてを try except または while ループに入れます。無限ループ (while ループの場合) のリスクを実行したり、コードを台無しにしたり (try except ステートメントの場合) することなく、モデル エンティティが常に取得されるようにするにはどうすればよいですか?