Python 使用ndb在另一种类型中多次引用单个Google数据存储类型

Python 使用ndb在另一种类型中多次引用单个Google数据存储类型,python,google-app-engine,google-cloud-datastore,endpoints-proto-datastore,Python,Google App Engine,Google Cloud Datastore,Endpoints Proto Datastore,我有以下2个ndb型号 from endpoints_proto_datastore.ndb import EndpointsModel class Foo(EndpointsModel): attr1 = ndb.StringProperty(required=True) class Bar(EndpointsModel): attr1 = ndb.KeyProperty('Foo', required=True) attr2 = ndb.KeyProperty('

我有以下2个ndb型号

from endpoints_proto_datastore.ndb import EndpointsModel

class Foo(EndpointsModel):
    attr1 = ndb.StringProperty(required=True)

class Bar(EndpointsModel):
    attr1 = ndb.KeyProperty('Foo', required=True)
    attr2 = ndb.KeyProperty('Foo', required=True)
正如您所看到的,Bar中有一些对Foo的引用

现在,当我为每个引用赋值时,第二个将替换第一个,并且只将其存储到db中,最有趣的是,当使用dev_appserver数据存储查看器进行查找时,该属性的名称为“Foo”,而不是第二个属性的名称,它替换了第一个属性

在我插入之后,这就是我所期望的

Bar(key=Key('Bar', xxxxxxxxxxxxxxxx), attr1=Key('Foo', xxxxxxxxxxxxxxxx), attr2=Key('Foo', xxxxxxxxxxxxxxxx)
但我只知道

Bar(key=Key('Bar', xxxxxxxxxxxxxxxxxx), attr2=Key('Foo', xxxxxxxxxxxxxxxx))
在数据存储查看器中

Entity Kind Bar

Entity Key  xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

ID          xxxxxxxxxxxxxxxx


Foo (Key)   xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Foo: id=xxxxxxxxxxxxxxxx

KeyProperty的第一个参数是属性的名称(如果希望名称与类属性不同),因此两次使用相同的名称将产生您看到的行为

您应该使用命名参数来指定类型:

ndb.KeyProperty(kind='Foo', required=True)

我刚刚测试了这个,得到了同样的结果。不知道为什么:\@Kris至少我有伴:)