本文介绍了从 NDB 中的 ComputedProperty 函数返回列表的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序转换为使用 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.

我可以在 ext.ndb 的 model.py 中看到 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.仅当您想要进行可能查询的计算时才使用计算属性.ComputedProperty 可以将其 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. 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 函数返回列表的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 11:41