小编yo2*_*ian的帖子

以下是NDB中未知类型的反向引用

我正在编写我的第一个RESTful Web服务,在GAE和Python 2.7运行时上面; 我开始使用Guido的闪亮的新ndb API.

但是,我不确定如何在没有原始db API的隐式反向引用功能的情况下解决特定情况.如果用户代理请求特定资源并且删除了1度的资源:

主机/ API /种/编号?深度= 2

鉴于相关实体的类型在开发时是未知的,在一对多关系中从"一"发现相关实体集合的最佳方法是什么?

  • 由于后一种限制,我无法使用之前的SO查询中描述的替换查询.我的模型在运行时可定义(因此不是硬编码)这​​一事实使我无法使用查询来过滤匹配键的属性.

  • 由于数据存储限制阻止我在没有指定类型的情况下对属性进行过滤,因此祖先和其他无类查询也会被淘汰.

到目前为止,我所拥有的唯一想法(除了恢复到db api之外)是使用跨组事务在"one"上编写我自己的引用,或者通过更新包含ndb.StringProperty(repeat = True)每当引入新类型的实体时,或者通过简单地在每次将相关的"多"实体写入数据存储区时维护"一"ndb.KeyProperty(repeat = True)上的键列表时,所有相关类型.

我希望比我更有经验的人能提出更好的方法.

鉴于jmort253的建议,我将尝试通过改编自文档的具体示例来扩充我的问题:

class Contact(ndb.Expando):
    """ The One """

    # basic info
    name = ndb.StringProperty()
    birth_day = ndb.DateProperty()

    # If I were using db, a collection called 'phone_numbers' would be implicitly 
    # created here.  I could use this property to retrieve related phone numbers 
    # when this entity was queried.  Since NDB lacks this feature, the service 
    # will …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine app-engine-ndb google-cloud-datastore

7
推荐指数
1
解决办法
796
查看次数