在使用ndb时,如何在另一个kind中多次引用单个Google datastore kind?

3

我有以下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相关的引用。

当我为每个引用分配值时,第二个引用替换了第一个引用,只有第二个引用存储到数据库中。最有趣的是,当使用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

1
我刚刚测试了一下,结果也是一样的。不知道为什么 : \ - Mmm Donuts
@Kris 至少我有伴了 :) - Afzal S.H.
1个回答

3
KeyProperty的第一个参数是属性名称(如果您希望名称与类属性不同),因此重复使用相同的名称将产生您所看到的行为。
您应该改用命名参数来指定种类:
ndb.KeyProperty(kind='Foo', required=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接