database - SDK Console显示内容,App抓取空列表

标签 database google-app-engine

我当前的应用程序使用 db.ListProperty(db.Key) 来实现好友列表的多对多关系。

我们有以下方案:

# previous models
class User(db.Model):
    name = db.StringProperty()
    nickname = db.StringProperty()
    join_date = db.DateProperty(auto_now_add=True)
    party = db.ReferenceProperty(PartyEntry, collection_name='participants')
    def by_name(self, string):
        return self.gql("WHERE name = :name", name=string).get()
    def key_by_name(self, string):
        return self.by_name(string).key()

class FriendList(db.Model):
    owner = db.ReferenceProperty(User)
    friends = db.ListProperty(db.Key)

还有一个获取用户好友列表、处理并显示它的处理程序:

class Profile_FriendList(WebBase):
    def get(self):
        user = self._auth()
        if user:
            usr_key = User().key_by_name(user.nickname())
            friend_list = friend_lists = FriendList.all().filter('owner', usr_key).fetch(10)
            self.renderskeleton( { 'friendlist': friend_list } ,'friends.html')

我在 FriendList.friends 中使用两个 db.Key() 列表条目填充了一个测试好友列表。 南卡罗来纳州SDK 控制台(>Interactive Datastorageviewer)向我显示这些项目:

key1,key2

但是如果我通过它的键调用这个实体 FriendList 并且 打印 frlst.friends 输出: [] 处理程序中存在同样的问题。 我没有可以使用的数据。 但数据存在。

已修复: FriendList 问题,被调用的 friendlist 对象不是查询,它只是一个新的 FriendList() 对象。 更新代码。

已修复:问题已解决

usr_key = User().key_by_name(user.nickname())
logging.info(user.nickname())
logging.info(usr_key)
friend_list = FriendList().all().filter("owner", usr_key).fetch(20)
logging.info(friend_list)
logging.info(friend_list[0].friends)
friends = User.get(friend_list[0].friends)
logging.info(friends)
self.renderskeleton( { 'friends': friends } , 'friends.html')

我选择了一个 GqlQuery 结果列表而不是我想要的“一个”结果,我必须从那里做的只是将 friend 的 key 列表放入 User.get() 中,瞧,我拥有了所有必要的数据。

最佳答案

friend_list = FriendList(owner=usr_key) 不是 query , 它正在创建 FriendList 的新实例。

你想要:friend_lists = FriendList.all().filter('owner', usr_key).fetch(10)

其他评论:

  1. 您用于查询的 User 上的两个方法实际上应该是类方法:

    @classmethod
    def by_name(cls, string):
        return cls.gql("WHERE name = :name", name=string).get()
    
    @classmethod
    def key_by_name(cls, string):
        return cls.by_name(string).key()
    
    # to call:
    user = User.key_by_name('their name')
    
  2. 您是否考虑过使用 user's idget_by_key_name() ?它会比查询更快。

  3. 同时使用用户的 id 作为好友列表的 key_name,然后您可以执行 friend_list = FriendList.get_by_key_name(str(user.user_id())) 而不是查询.您需要一种方法来处理一个 FriendList 不够用的情况,但这种情况可能很少见——而且解决起来并不难(向 key_name 添加一个序列号并存储(非索引)计数User 类上的序列,然后您可以轻松生成 key_names)。

关于database - SDK Console显示内容,App抓取空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4897340/

相关文章:

php - 为什么在 cpanel 数据库大小显示 0.00MB?

python - AuthSub 问题

Java 和 Python 在单个 Google App Engine 项目中一起使用

mysql无法创建表errno 150

c# - 如何禁用 EF 代码优先中链接表的级联删除?

database - 查询具有唯一哈希属性的 DynamoDB?

node.js - 在 nodejs session 中使用 google memcache

java - 将文件上传到 Blob 存储时,Google App Engine 和自定义域出现 CORS 问题

python - Google App Engine URL 处理程序运行时出错

mysql - 如何将mysql的默认端口从3306更改为3360