从 NDB 中的 ComputedProperty 函数返回列表的解决方法
我正在将我的应用程序转换为使用 NDB。我以前曾经有过这样的事情:
@db.ComputedProperty
def someComputedProperty(self, indexed=False):
if not self.someCondition:
return []
src = self.someReferenceProperty
list = src.list1 + src.list2 + src.list3 + src.list4 \
+ [src.str1, src.str2]
return map(lambda x:'' if not x else x.lower(), list)
正如你所看到的,我生成列表的方法有点复杂,我更喜欢保持这种方式。但是当我开始转换为 NDB 时,我只是将 @db.CompulatedProperty
替换为 @model.CompulatedProperty
但随后出现此错误:
NotImplementedError: Property someComputedProperty does not support <type 'list'> types.
我可以在 model.txt 中看到。 py
在 ext.ndb 中,CompulatedProperty
继承自 GenericProperty
,其中在 _db_set_value
中有几个 if/else 语句根据其类型处理值,但没有对列表进行处理
当前它会经历第一个条件,并在我返回空列表时给出该错误。
有没有办法解决这个问题并避免错误?
I am converting my app to use NDB. I used to have something like this before:
@db.ComputedProperty
def someComputedProperty(self, indexed=False):
if not self.someCondition:
return []
src = self.someReferenceProperty
list = src.list1 + src.list2 + src.list3 + src.list4 \
+ [src.str1, src.str2]
return map(lambda x:'' if not x else x.lower(), list)
As you can see, my method of generating the list is a bit complicated, I prefer to keep it this way. But when I started converting to NDB, I just replaced @db.ComputedProperty
by @model.ComputedProperty
but then I got this error:
NotImplementedError: Property someComputedProperty does not support <type 'list'> types.
I could see in model.py
in ext.ndb that ComputedProperty
inherits from GenericProperty
where in the _db_set_value
there are several if/else statements that handle value according to its type, except that there's no handling for lists
Currently it goes through the first condition and gives out that error when I return an empty list.
Is there a way to work around this and avoid the error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要为 NDB 中的计算属性设置repeated=True 标志。我不认为你可以使用可爱的“@db.CompulatedProperty”符号,你必须说:
You need to set the repeated=True flag for your computed property in NDB. I don't think you can use the cute "@db.ComputedProperty" notation, you'll have to say:
整个功能可以在函数内完成,因此它不需要是
CompulatedProperty
。仅当您想要执行可能查询的计算时才使用计算属性。CompatedProperty
可以将其indexed
标志设置为False
但这意味着您不会查询它,因此实际上不会需要将其作为财产。This whole functionality can be done within a function, so it doesn't need to be a
ComputedProperty
. Use Computed Properties only when you want to do a computation that you might query for. AComputedProperty
can have itsindexed
flag set toFalse
but then this means you won't be querying for it, and therefore don't really need to have it as a property.