App Engine 中如何应用默认值?

How are default values applied in app engine?

我想知道默认值是在实例创建、实例更新时应用,还是仅在尝试读取值时应用。我想了解这如何应用于查询。

例如,假设我最初创建了一个模型w/o默认值

class Foo (db.Model):
   bar = db.BooleanProperty()

然后我创建 put Foo 的几个实例。

然后我更新模型

class Foo (db.Model):
   bar = db.BooleanProperty(default=False)

然后我有一个问题

foos = Foo.all().filter('bar =', False)

结果是否会包括在将默认值添加到模型定义之前创建的 Foo 实例?

如果我改为查询

foos = Foo.all().filter('bar !=', True)

这是否包括在将默认值添加到模型定义之前创建的 Foo 实例?它(在布尔值 属性 的情况下)和 wrt 默认值与之前的查询有什么不同吗?

我查找了这方面的文档,但找到了 none,抱歉。希望我只是想念他们。如果可能,请提供指向 GAE 文档的链接。

更新: 我在文档中找到了这个

Entities lacking a property named in the query are ignored

Entities of the same kind need not have the same properties. To be eligible as a query result, an entity must possess a value (possibly null) for every property named in the query's filters and sort orders. If not, the entity is omitted from the indexes used to execute the query and consequently will not be included in the query's results.

Note: It is not possible to query for entities that are specifically lacking a given property. One alternative is to define a fixed (modeled) property with a default value of None, then filter for entities with None as the value of that property.

https://cloud.google.com/appengine/docs/python/datastore/queries

假设在创建或更新实体时应用了默认值,我认为这意味着上述两个查询都不会 return 在将默认值添加到模型之前最后放置的实体.是吗?

谢谢

这真的取决于。如果您实际上在拥有默认值之前将 "bar" 放入您的 foo 中。

1- 如果您从未设置 bar 而只设置 "put" foos,那么是的,在添加默认值之前放置的任何内容都将没有任何内容,因为它们的 "bar".

2- 如果您在创建 foo 时实际以编程方式设置栏,那么您的查询将 return 一切。

你表达问题的方式让我相信你已经用 #1 设置了它,所以不,你在默认值之前放入数据存储的任何东西都不会 returned。您将需要遍历整个数据存储并更新您的 foos。

虽然官方文档中没有,但 Alex Martelli(Google 员工)

在评论中对此进行了验证

默认值仅在实体创建时保持不变时分配。更新将保持原始值。

此默认值不会以任何方式影响查询,保存实体时会写入索引,因此您需要使用正确的值再次写入实体。