问题描述
我有一个User ndb.Model,它的用户名StringProperty允许使用大写小写字母,在某些时候我想通过用户名来获取用户,但必须将大小写强制为小写以进行过滤.为此,我向用户添加了一个ComputedProperty:username_lower,它返回用户名的小写形式,如下所示:
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过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!