小编yas*_*ser的帖子

遵循AppEngine上的数据存储模型结构 - 按日期订购关注者

在我的应用中,用户可以关注其他用户,并在他们关注的人员执行活动时获得更新.

我以这种方式存储以下关系:

class User(db.Model):
  ''' User details '''
  username = db.StringProperty()

class Contacts(db.Model):
    '''Store users contacts
       parent= User (follower)
       key_name= Users username (follower)
       contacts = A list of keys of Users that a User follows '''
    contacts = db.ListProperty(db.Key)
    last_updated = db.DateTimeProperty(auto_now=True)
Run Code Online (Sandbox Code Playgroud)

获取关注者和用户关注的用户(关注者和关注者):

'''Get Users that my_user follows'''
my_user = User().all().fetch(1)
contacts = Contacts.get_by_key_name(my_user.username).contacts

''' get my_user followers - copied from an answer here on stackoverflow '''
follower_index = models.Contacts.all(keys_only=True).filter('contacts =',my_user)
follower_keys = [f.parent() for f in follower_index]
followers = …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine social-networking

5
推荐指数
1
解决办法
570
查看次数

从分片计数器更新实体 - Python

在我的appengine python应用程序中,我有分片计数器,用于计算照片的favs数量和视图数量.

我有照片和计数器型号.要按受欢迎程度(最喜欢的数量)订购照片,我应该在我的照片实体(Photo.all().order('num_favs'))中存储最喜欢的#.由于我正在跟踪分片计数器中的fav的数量,我想知道用Counter模型中最新的#fores来更新我的Photo实体的最有效方法是什么?

这是我的模型的样子摘要:

class Photo(db.Model):
  photo = db.BlobProperty() 
  favs = db.IntegerProperty() #num of favs

class Counter(db.Model):
  #key_name = key to a model that's to be counted - Photo in this case
  name =  db.StringProperty() #counted entity key
  count = db.IntegerProperty() #count
  type = db.StringProperty() #type of model to count, "Photo" in this example

#Very unefficient way of updating my Photo entities with the latest count from Counter:

fav_counters = Counter.all().filter('type =', 'Photo')

for fav in fav_counters:
  photo_fav = db.get(fav.name) …
Run Code Online (Sandbox Code Playgroud)

python google-app-engine

4
推荐指数
1
解决办法
393
查看次数