我想設計表來建立一個跟隨者的關係。如何在appengine中建模follower流?
說我有一個140char的記錄流,有用戶,標籤和其他文本。
用戶關注其他用戶,也可以按照標籤。
我在概述我設計下面的方式,但在我的設計中有兩個限制。我想知道其他人是否有更聰明的方法來實現相同的目標。
這樣做的問題是
- 追隨者清單中被複制在每個記錄
- 如果一個新的跟隨者添加或刪除一個,「全部」的 記錄必須更新。
代碼
class HashtagFollowers(db.Model):
"""
This table contains the followers for each hashtag
"""
hashtag = db.StringProperty()
followers = db.StringListProperty()
class UserFollowers(db.Model):
"""
This table contains the followers for each user
"""
username = db.StringProperty()
followers = db.StringListProperty()
class stream(db.Model):
"""
This table contains the data stream
"""
username = db.StringProperty()
hashtag = db.StringProperty()
text = db.TextProperty()
def save(self):
"""
On each save all the followers for each hashtag and user
are added into a another table with this record as the parent
"""
super(stream, self).save()
hfs = HashtagFollowers.all().filter("hashtag =", self.hashtag).fetch(10)
for hf in hfs:
sh = streamHashtags(parent=self, followers=hf.followers)
sh.save()
ufs = UserFollowers.all().filter("username =", self.username).fetch(10)
for uf in ufs:
uh = streamUsers(parent=self, followers=uf.followers)
uh.save()
class streamHashtags(db.Model):
"""
The stream record is the parent of this record
"""
followers = db.StringListProperty()
class streamUsers(db.Model):
"""
The stream record is the parent of this record
"""
followers = db.StringListProperty()
Now, to get the stream of followed hastags
indexes = db.GqlQuery("""SELECT __key__ from streamHashtags where followers = 'myusername'""")
keys = [k,parent() for k in indexes[offset:numresults]]
return db.get(keys)
有沒有一種更聰明的辦法做到這一點?
Duplicate http://stackoverflow.com/questions/2668470/good-way-of-implementing-a-twitter-like-follower-system – 2010-05-24 18:47:13