问题描述
我正在将我的应用转换为使用NDB。我以前有过这样的东西:
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)
正如您所看到的,我的生成列表的方法有点复杂,我更喜欢用这种方式来保存它。但是当我开始转换为NDB时,我只是用 @ model.ComputedProperty 取代了 @ db.ComputedProperty ,但后来我得到了这个错误:
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.
我可以在 model.py 中看到ext.ndb ComputedProperty 继承于 GenericProperty 其中 _db_set_value 除了没有处理 lists
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?
推荐答案
这个完整的功能可以在一个函数内完成,所以它不需要是 ComputedProperty 。仅当您想要执行可能查询的计算时才使用Computed Properties。 ComputedProperty 可以将其索引的标志设置为 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. A ComputedProperty can have its indexed flag set to False but then this means you won't be querying for it, and therefore don't really need to have it as a property.
def someComputedProperty(self): 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中从ComputedProperty函数返回列表的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!