订购查询

时间:2017-02-14 20:49:43

标签: python google-cloud-datastore app-engine-ndb datastore

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

我想用这些查询进行一次查询,因为我想在整个配置文件集合中使用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

1 个答案:

答案 0 :(得分:0)

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

# 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?

相关问题