【问题标题】:How to assign default value to all NDB datastore entries?如何为所有 NDB 数据存储条目分配默认值?
【发布时间】:2013-11-07 19:56:34
【问题描述】:

我必须向现有的 NDB 类添加一个新属性:

class AppList(ndb.Model):
    ...
    ignore = ndb.BooleanProperty(default=False) # new property

然后我会像下面这样使用它:

entries = AppList.query()
entries = entries.filter(AppList.counter > 5)
entries = entries.filter(AppList.ignore == False)
entries = entries.fetch()

我不能使用AppList.ignore != True 来捕获早期添加的记录(没有ignore 属性),所以我必须为我的AppList 实体中的所有记录分配False。最有效的方法是什么?目前,该实体包含大约 4'000 个条目。

更新。我决定使用以下丑陋的代码(没有设法应用cursors),它作为一个cron作业运行。但是我不是每次都更新相同的 100 条记录吗?

entities = AppList.query()
# entities = entities.filter(AppList.ignore != False)
entities = entities.fetch(100)
while entities:
    for entity in entities:
        entity.ignore = False
        entity.put()
    entities = AppList.query()
    # entities = entities.filter(AppList.ignore != False)
    entities = entities.fetch(100)

【问题讨论】:

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


    【解决方案1】:

    不要忘记在这些情况下使用了一个 MapReduce 库。但我认为最好的方法是结合使用所有这些建议。

    现在,您需要 get() 和 put() 4000 个实体,问题是如何降低此操作的“成本”。

    我只是想知道您的bool(entity.ignore) 会返回什么。如果missing 属性返回 False,您可以调整代码,将其视为 False 并推迟操作。如果你 put() 出于其他原因属性ignore 被写入False,这要归功于默认参数。因此,对于其余实体可以运行这样的脚本(通过 remote_api):

    def iter_entities(cursor=None):
        entries = AppList.query()
        res, cur, more = entries.fetch_page(100, start_cursor=cursor)
        put_queue = [ent for ent in res if not hasattr(ent, 'ignore')]
        # put_queue = []
        # for ent in res:
        #    if not hasattr(ent, 'ignore'):
        #        put_queue.append(ent)
        ndb.put_multi(put_queue)
        if more:
            iter_entities(cur) # a taskqueue is better
    

    【讨论】:

    • 谢谢。您能否澄清以下行的作用:put_queue = [ent for ent in res if not hasattr(ent, 'ignore')]?如果条目没有“忽略”属性,那又是什么?
    • 回答已编辑:ndb.get_multi() 需要一个实体列表。该行创建了一个列表,其中仅包含没有“忽略”属性的实体。它与 4 条新线的作用相同。
    • 嗯。我是否理解正确 - 您的代码实际上没有将默认值 False 分配给 ignore 属性? IE。在put_queue.append(ent) 之前缺少一行ent.ignore = False?
    • put() 或 put_multi() 设置为每个缺失值的默认参数。如果模型中没有默认参数,则需要该行。
    • 那很奇怪。因为我已经在我的数据库上运行了你的代码,但它没有用这个缺失值更新条目......而且,是的,我在模型中有 default=False 参数。
    【解决方案2】:

    您更新后的代码将仅更新前 100 个实体。尝试使用光标

    https://developers.google.com/appengine/docs/python/ndb/queries#cursors

    如果你不能使用游标,那么使用偏移量并在每个循环中将偏移量增加 100 或通过 fetch() 获取所有条目一次(游标方法更好)

    而不是一一使用 ndb.put_multi(要放置的实体列表)

    这样会比一个一个放的快

    【讨论】:

    • 谢谢。我尝试使用cursors,但我不明白start_cursor 的值应该是什么,当我将它称为第二/第三等时间时。
    • 每次使用游标进行查询时,都会得到一组数据和一个游标,直到到达数据末尾且没有游标(或者游标为None,我忘了哪个)。
    【解决方案3】:

    您可以尝试使用 hasattr 检查记录是否具有忽略属性。

    如果您只想为 AppList 实体中的所有记录分配 False,您只需更新架构(重新加载 models.py 文件),然后您应该能够将属性设置为 False。

    有关架构更新的更多信息,请访问here.

    编辑: 回答您的评论:

    if hasattr(entity, 'ignore'):
      #your code goes here
    

    【讨论】:

    • 如何将hasattr 与fetch 一起使用?请参阅我更新的问题 - 主要问题是如何获取没有此属性的实体。
    【解决方案4】:

    不部署新代码的最简单方法是使用远程 api 并执行查询以获取所有实体并将属性值设置为 false,然后 put() 它们。 4000 条记录并不多。

    其实你甚至不需要显式设置该值,如果当前没有值,则在检索时将其设置为默认值。

    【讨论】:

    • 不幸的是,看起来我必须设置值 - 请参阅我的问题,如果我使用 query,那么它不会“应用”默认值。
    • 哦,这只是对结果集的简单 for 循环,设置值并放置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2020-01-08
    • 2022-01-04
    相关资源
    最近更新 更多