如何查询多对多关系模型? - 谷歌应用程序引擎
这是我的模型:
class User(db.Model):
id = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
name = db.StringProperty(required=True)
email = db.StringProperty()
class Page(db.Model):
id = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
name = db.StringProperty(required=True)
link = db.StringProperty(required=True)
class UserPage(db.Model):
user = db.ReferenceProperty(User, collection_name='pages')
page = db.ReferenceProperty(Page, collection_name='users')
如何构建查询来查找用户页面?
我发现一篇文章描述了一种方法,但这是最好的方法吗? http://blog.arbingersys。 com/2008/04/google-app-engine-better-many-to-many.html
Here are my models:
class User(db.Model):
id = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
name = db.StringProperty(required=True)
email = db.StringProperty()
class Page(db.Model):
id = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
name = db.StringProperty(required=True)
link = db.StringProperty(required=True)
class UserPage(db.Model):
user = db.ReferenceProperty(User, collection_name='pages')
page = db.ReferenceProperty(Page, collection_name='users')
How would I construct a query to find a users pages?
I found an article that describes a method to do it but is this the best way? http://blog.arbingersys.com/2008/04/google-app-engine-better-many-to-many.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的答案会起作用,但它将执行 7 次对数据存储的调用:
另一种仅对数据存储进行 3 次调用的方法如下所示:
请参阅 http://blog.notdot.net/2010/01/ReferenceProperty-prefetching-in-App-Engine 了解更多详细信息。
Your answer will work, but it will perform 7 calls to the datastore:
An alternative approach which only does 3 calls to the datastore would be something like this:
See http://blog.notdot.net/2010/01/ReferenceProperty-prefetching-in-App-Engine for more details.
经过一些测试,我似乎可以使用:
After some testing, it appears I can use:
我会推荐一种不同的方法,它比您的
UserPage
关系更少“面向关系”:然后您可以使用以下查询获取特定用户的所有页面:
请注意,您应该将列出您期望项目较少的一侧的键属性。我假设您喜欢某个页面的用户数量少于链接到用户的页面,因此我将其放在
Page
一侧,但这取决于您的具体用例。请参阅此处,了解有关多对多主题的官方建议:http://code. google.com/appengine/articles/modeling.html
I would recommend a different approach, that is less "relational-oriented" than your
UserPage
relationship:And then you can get all pages of a specific user with the following query:
Please note that you should place the list property of keys on the side where you expect less items. I've assumed you will have less users liked to a page, than pages linked to a user, so I've put it on the
Page
side, but that will depend on your specific use case.See here for official recommendations on the many-to-many topic: http://code.google.com/appengine/articles/modeling.html