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

查看:148
本文介绍了如何在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天全站免登陆