排序查询

2024-05-16 23:07:36 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的这组查询将一组配置文件连接在一起,以返回到一个网站

我想用它们做一个查询,因为我想对整个概要文件集合使用order函数

# Get all users for who 'currentUser' is an assistant for.
users = Users.query(Users.assistant.IN(currentUser)).fetch()

# Get all profiles that belong to those users and have not been deleted.
for user in users:
    profiles = profiles + Profiles.query(
            ndb.AND(
            Profiles.user == user.username,
            Profiles.deleted == False
        )
    ).order(Profiles.history).fetch() # Order all profiles by timestamp

Tags: forget配置文件orderfetchallqueryprofiles
1条回答
网友
1楼 · 发布于 2024-05-16 23:07:36

在某种程度上,你所寻找的将相当于:

# get the list of usernames obtained above
usernames = [user.username for user in users]

profiles = Profiles.query(ndb.AND(Profiles.user.IN(usernames),
                                  Profiles.deleted == False))
                         .order(Profiles.history).fetch()

但是,如果usernames包含许多元素(显然超过10个左右),则会出现性能问题,请参见Does the NDB membership query ("IN" operation) performance degrade with lots of possible values?

相关问题 更多 >