简体   繁体   English

3个模型实体加入AppEngine Python

[英]3 Models Entity Joins in AppEngine Python

I've read a lot about how I need to rethink implementing entities and joins when developing for AppEngine, and I still can't figure out how to do this: 我已经读了很多关于在为AppEngine开发时如何重新考虑实现实体和联接的知识,但我仍然不知道该怎么做:

I've 3 entities. 我有3个实体。 Tag has many Tweets and User Follow many Tags . 标签有很多推文,用户关注许多标签

I would like to get all Tweets that is Tagged with tags that a User follows. 我想获取所有带有用户关注的标签标记的推文。

I thought eliminating Tags entity and using StringListProperty might work as this: 我认为消除标签实体并使用StringListProperty可能会这样工作:

Tweets.all().filter(‘tag IN”, user.tags) 

However, as in the ListProperty documentations indicated, this won't work! 但是,如ListProperty 文档中所述,这将不起作用! as because a query can't compare two lists. 因为查询无法比较两个列表。

A query cannot compare two list values. 查询不能比较两个列表值。 There is no way to test two lists for equality without testing each element for membership separately. 如果不分别测试每个元素的成员资格,就无法测试两个列表是否相等。

Any idea how to structure the entities in order I can be able to answer the question: 任何想法如何构造实体以便我都能回答这个问题:

How to get all Tweets that is Tagged with tags that a User follows? 如何获取所有带有用户关注标签的 Tweet?

Thanks 谢谢

You're not trying to compare two lists - you're checking for intersection. 您不是要比较两个列表,而是要检查交叉点。 The query you list will do that, with one major caveat: IN filters are split up into multiple sub-queries, which are executed independently by the backend, with a maximum of 30 sub-queries. 您列出的查询将做到这一点,但有一个主要警告:IN过滤器分为多个子查询,这些子查询由后端独立执行,最多有30个子查询。

That is, if your user follows 3 tags ['foo', 'bar', 'baz'], doing the above is equivalent the union of: 也就是说,如果您的用户遵循3个标签['foo','bar','baz'],则执行上述操作等效于以下操作的并集:

Tags.all().filter('tag =', 'foo')
Tags.all().filter('tag =', 'bar')
Tags.all().filter('tag =', 'baz')

You should find this article on managing entity relationships helpful. 您应该发现有关管理实体关系的这篇文章很有帮助。

Since each Tweet can have more than one tag, you'll need to follow the "Many to Many" section of this article. 由于每个Tweet可以具有多个标签,因此您需要遵循本文的“多对多”部分。 You'll probably want do the same thing for relationships between Users and Tags. 您可能希望对用户和标签之间的关系做同样的事情。 This will then give you an efficient way to get all Tweets corresponding to Tags followed by a User. 然后,这将为您提供一种有效的方式来获取与用户之后跟随的标签相对应的所有推文。

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

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