问题描述
我有一个用户 ndb.Model,它的用户名 StringProperty 允许大写和小写字母,在某些时候我想通过用户名获取用户,但为了过滤而强制使用小写字母.因此,我向 User: username_lower 添加了一个 ComputedProperty,它返回用户名的小写版本,如下所示:
I have a User ndb.Model which has a username StringProperty that allows upper en lower case letters, at some point I wanted to fetch users by username but have the case forced to lowercase for the filtering. Therefor I added a ComputedProperty to User: username_lower which returns the lowercase version of the username as follows:
@ndb.ComputedProperty
def username_lower(self):
return self.username.lower()
然后我像这样过滤查询:
then I filter the query like so:
query = query.filter(User.username_lower==username_input.lower())
这有效,但它只对创建(放置)之后的用户有效,我将其添加到模型中.之前创建的用户不会被此查询过滤.我首先认为 ComputedProperty 对老用户不起作用.但是,尝试此操作并在旧用户上调用 .username_lower 确实有效.
This works, however it only does for users created (put) after I added this to the model. Users created before don't get filtered by this query. I first thought the ComputedProperty wasn't working for the older users. However, tried this and calling .username_lower on an old user does work.
最后,我找到了一个解决方案是获取所有用户并运行一个 .put_multi(all_users)
Finally, I found a solution to this is to fetch all users and just run a .put_multi(all_users)
因此,当您直接调用它但最初没有过滤时,似乎稍后添加到模型中的 ComputedProperty 可以工作.它不会自动编入索引吗?或者它可能是一个缓存的东西..?
So seems like a ComputedProperty added later to the model works when you invoke it straight but doesn't filter at first. Does it not get indexed automatically ? or could it be a caching thing.. ?
欢迎任何有关其行为如此的见解
any insight to why it was behaving like this would be welcome
谢谢
推荐答案
这是预期的行为.当对象被放置"时,ComputedProperty(或我猜想的任何属性)的值被索引.数据存储不执行自动模式更新或类似的操作.当您更新架构时,您需要在代码中允许不同的架构版本或单独更新您的实体.在索引更改的情况下,您别无选择,只能更新您的实体.MapReduce API 可用于更新实体以避免请求限制和喜欢.
this is the expected behaviour. The value of a ComputedProperty (or any property for that matter I guess) is indexed when the object is "put". The datastore does not do automatic schema updates or anything like that. When you update your schema you need to either allow for different schema versions in your code or update your entities individually. In the case of changes to indexing you have no choice but to update your entities. The MapReduce API can be used for updating entities to avoid request limitations and the like.
这篇关于ndb ComputedProperty 过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!