ndb.KeyPropertyキーだけでなく、エンティティまたはベース 64 キー文字列を受け入れてキーに変換できるサブクラスを作成しました。プロパティを作成しようとする場合を除いて、うまく機能しrepeatedます。
基本的に私のコードはこれです:
def to_key(target):
if isinstance(target, ndb.Model):
target_key = target.key
elif isinstance(target, ndb.Key):
target_key = target
else:
try:
target_key = ndb.Key(urlsafe=target)
except:
raise TypeError('%s is not an ndb instance or key' % target)
return target_key
class AutoKeyProperty(ndb.KeyProperty):
def _validate(self, value):
return to_key(value)
反復可能な値の処理_validateは、基本ndb.KeyPropertyクラスまで問題に移動するだけです。