简体   繁体   English

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

[英]Referring a single Google datastore kind multiple times in another kind with ndb

I have the below 2 ndb models 我有以下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)

As you see Bar has a couple of references to Foo. 如你所见,Bar有几个对Foo的引用。

Now when I assign values to each of the references, the second one replaces the first and only it gets stored to the db and the funniest part is that when looked up with the dev_appserver datastore viewer, the property is under the name 'Foo', not under the second property's name, that replaced the first one. 现在,当我为每个引用赋值时,第二个替换第一个,只有它存储到db,最有趣的部分是当使用dev_appserver数据存储查看器查找时,属性名为'Foo',不在第二个属性的名称下,取代了第一个属性。

After I insert this is what I expect 插入后,这是我所期待的

Bar(key=Key('Bar', xxxxxxxxxxxxxxxx), attr1=Key('Foo', xxxxxxxxxxxxxxxx), attr2=Key('Foo', xxxxxxxxxxxxxxxx)

but I only get 但我只能得到

Bar(key=Key('Bar', xxxxxxxxxxxxxxxxxx), attr2=Key('Foo', xxxxxxxxxxxxxxxx))

And in the datastore viewer, 在数据存储区查看器中,

Entity Kind Bar

Entity Key  xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

ID          xxxxxxxxxxxxxxxx


Foo (Key)   xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Foo: id=xxxxxxxxxxxxxxxx

The first argument to KeyProperty is the name for the property (if you want to have the name be different to the class-property), so using the same name twice will produce the behaviour you're seeing. KeyProperty的第一个参数是属性的名称(如果您希望名称与class-property不同),因此使用相同的名称两次将产生您所看到的行为。

You should use a named argument instead to specify the kind: 您应该使用命名参数来指定类型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM