Google App Engine:以多对多关系对集合进行排序

时间:2011-07-09 10:49:35

标签: google-app-engine many-to-many google-cloud-datastore modeling

考虑以下模型:

class Tweet(db.Model):
    text = db.StringProperty
    created_at = db.DateTimeProperty()

class Company(db.Model):
    short_name = db.StringProperty()
    full_name = db.StringProperty()
    oneword_name = db.StringProperty()

class TweetMentionsCompany(db.Model):
    tweet = db.ReferenceProperty(Tweet, required=True,
                        collection_name='mentions_companies')
    company = db.ReferenceProperty(Company, required=True, 
                        collection_name='tweets_mentioned_in')

如果我有一个Company.oneword_name的值,我如何按时间顺序获取相关公司最近发布的100条推文的列表?

我当前的代码如下所示,但我不确定如何按时间顺序修改:

company = models.Company().all().filter('oneword_name = ', oneword_name.lower()).get()

for e,tmc in enumerate(company.tweets_mentioned_in):
    if e>100: break
    print_tweet_lite(self, tmc.tweet)

2 个答案:

答案 0 :(得分:1)

您必须在created_at = db.DateTimeProperty()中添加字段TweetMentionsCompany,然后按其排序。

这是NoSQL,“数据库规范化”在这里效果不佳

答案 1 :(得分:1)

在数据存储区中无法进行基于“连接”的查询。如果我是你,那么我要做的是在[ListProperty][1]上创建Tweet,其中包含所提到公司的列表 - 这个列表可以在您计划计算{的同时轻松计算{1}}实体。

然后,您可以TweetMentionsCompany filter('company = ', company)查找提及公司的推文列表(如果列表中的任何公司与给定公司匹配,则会匹配)并按您想要的任意方式对其进行排序。

相关问题