在 App Engine for Python 中,是否可以保留一个类,并在其中嵌套另一个对象?
在 App Engine for Python 中,是否有类似 Objectify(Java 库)之类的东西,我可以轻松地将一个类嵌入到另一个类中并将其保存到数据存储区?
此类的建模方式类似于以下示例,其中 Venue 包含 Location 对象。我想将其保留为一个嵌套对象,并且能够通过嵌入对象中的字段进行查询。
类位置():
城市 = db.StringProperty()
state = db.StringProperty()
类地点(db.Model):
名称 = db.StringProperty()
location = Location()
以下是有关它如何在 App Engine for Java 的 Objectify 中工作的信息。
http://code.google.com/p/objectify-appengine/ wiki/IntroductionToObjectify#@Embedded
这可以使用 Python 实现吗?
In App Engine for Python, is there anything like Objectify (Java Library) where I can easily embed a class within another and save it to the datastore?
This class would be modeled like the following example where a Venue contain a Location object. I want to persist this as one nested object, as well as be able to query by fields in the embedded object.
class Location():
city = db.StringProperty()
state = db.StringProperty()
class Venue(db.Model):
name = db.StringProperty()
location = Location()
Here is information on how it works in Objectify in App Engine for Java.
http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#@Embedded
Is this possible using Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑使用参考属性。即,将 Location 对象存储为其自己的实体,并通过引用将该位置合并到 Venue 对象中。
然后,如果您想同时在位置和场所进行交易,请使用数据存储事务。
编辑:要查询“包含”对象中的字段,请使用数据存储“反向引用”。也就是说,Venue 包含对 Location 的引用,这意味着 Location 也包含对 Venues 的引用。请参阅:http://code.google.com/appengine/docs /python/datastore/datamodeling.html#参考
Consider using Reference properties. I.e. store a Location object as its own entity and incorporate that location into the Venue object by reference.
Then, if you want to transact on a Location and Venue at the same time, use datastore transactions.
EDIT: To query fields in the 'contained' object, use datastore "back references". I.e. the fact that Venue contains a reference to Location means Location also contains references to Venues. See: http://code.google.com/appengine/docs/python/datastore/datamodeling.html#References
目前尚不支持,但 NDB 库 支持通过序列化将模型相互嵌入将它们作为协议缓冲区,或者通过嵌套它们的属性(Objectify 方式)。
Not currently, but the NDB library supports embedding models within one another either by serializing them as Protocol Buffers, or by nesting their properties (Objectify fashion).