如何在 NDB 中查询新创建的属性? [英] How to query for newly created property in NDB?

查看:34
本文介绍了如何在 NDB 中查询新创建的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前有过 NDB 类并且刚刚添加了新属性 receive_news:

I had NDB class before and added new property receive_news just now:

class User(ndb.Model):
    '''
    Index
        Key: user_id
    '''
    lang = ndb.StringProperty(required=None, indexed=True)
    receive_news = ndb.BooleanProperty(required=None, indexed=True)

我想获取想要接收我的新闻的用户列表(当前所有用户).我尝试了以下选项:

I would like to get list of users, who would like to receive my news (all users currently). I tried the following options:

keys = User.query(User.receive_news != True).fetch(keys_only=True)
keys = User.query(User.receive_news == None).fetch(keys_only=True)

两者都返回 0.我应该如何正确使用这个新属性?

both returns 0. How should I work properly with this new property?

推荐答案

数据存储索引仅在实体写入数据存储时更新,因此只有在创建新索引后写入的实体才会被更新添加到它.

Datastore indexes are only updated when entities are being written to the datastore, so only entities written after the new index was created will be added to it.

要将预先存在的实体添加到索引中(以便查询可以找到它们),您必须获取然后重新编写它们.也许沿这些路线使用一些东西(如果它们太多,您必须将其拆分为单独的请求/任务)

To have the pre-existing entities added to the index (so that they can be found by the query) you'll have to get and then re-write them. Maybe using something along these lines (you'll have to split it in separate requests/tasks if they are too many)

keys = []
for user in ndb.get_multi(User.query().fetch(keys_only=True)):
    if  user.receive_news is None:
        user.receive_news = True
        keys.append(user.key)
ndb.put_multi(keys)

这篇关于如何在 NDB 中查询新创建的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆