开发者

Google App Engine: Sorting collections in a many-to-many relationship

consider the following model:

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_com开发者_StackOverflowpanies')
    company = db.ReferenceProperty(Company, required=True, 
                        collection_name='tweets_mentioned_in')

If I have a value for Company.oneword_name, how can I get a list of the 100 most recent tweets the related company is mentioned in, in chronological order?

My current code looks something like the following, but I am not sure how to modify for chronological order:

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)


You have to add field created_at = db.DateTimeProperty() into your's TweetMentionsCompany, and sort by it.

It's NoSQL, "database normalization" isn't working well here


That kind of 'join' based query isn't possible in the datastore. If I were you, what I would do is create a [ListProperty][1] on Tweet that contains a list of the companies mentioned - this list can be easily computed at the same time as you were planning to compute the TweetMentionsCompany entities.

You can then do filter('company = ', company) on Tweet to find the list of tweets that mention a company (this will match if any company in the list matches the given company) and sort it any way you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜