yo2*_*ian 7 python google-app-engine app-engine-ndb google-cloud-datastore
我正在编写我的第一个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 neither have a reference to query nor the means to know the
# relationship exists in the first place since it cannot be hard-coded. The
# data model is extensible and user-defined at runtime; most relationships
# will be described only in the data, and must be discoverable by the server.
# In this case, when Contact is queried, I need a way to retrieve the
# collection of phone numbers.
# Company info.
company_title = ndb.StringProperty()
company_name = ndb.StringProperty()
company_description = ndb.StringProperty()
company_address = ndb.PostalAddressProperty()
class PhoneNumber(ndb.Expando):
""" The Many """
# no collection_name='phone_numbers' equivalent exists for the key property
contact = ndb.KeyProperty(kind='Contact')
number = ndb.PhoneNumberProperty()
Run Code Online (Sandbox Code Playgroud)
有趣的问题!所以基本上你想查看Contact类,看看是否有其他模型类有一个KeyProperty引用它; 在这个例子中PhoneNumber(但可能有很多).
我认为解决方案是要求用户在创建PhoneNumber类时显式添加此链接.
通过为用户提供KeyProperty的子类来处理这个问题,您可以轻松地为用户提供这些功能.例如
class LinkedKeyProperty(ndb.KeyProperty):
def _fix_up(self, cls, code_name):
super(LinkedKeyProperty, self)._fix_up(cls, code_name)
modelclass = ndb.Model._kind_map[self._kind]
collection_name = '%s_ref_%s_to_%s' % (cls.__name__,
code_name,
modelclass.__name__)
setattr(modelclass, collection_name, (cls, self))
Run Code Online (Sandbox Code Playgroud)
具体如何你选择集合的名称和存储的价值取决于你; 只需放置一些东西,便于您按照链接返回.该示例将在Contact上创建一个新属性:
Contact.PhoneNumber_ref_contact_to_Contact == (PhoneNumber, PhoneNumber.contact)
Run Code Online (Sandbox Code Playgroud)
[编辑以使代码工作并添加示例.:-)]
| 归档时间: |
|
| 查看次数: |
796 次 |
| 最近记录: |