从 NDB 中的 ComputedProperty 函数返回列表的解决方法

发布于 2024-12-27 10:49:11 字数 910 浏览 2 评论 0原文

我正在将我的应用程序转换为使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与之呼应 2025-01-03 10:49:11

您需要为 NDB 中的计算属性设置repeated=True 标志。我不认为你可以使用可爱的“@db.CompulatedProperty”符号,你必须说:

def _computeValue(self):
    ...same as before...
someComputedProperty = ComputedProperty(_computeValue, repeated=True, indexed=False)

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:

def _computeValue(self):
    ...same as before...
someComputedProperty = ComputedProperty(_computeValue, repeated=True, indexed=False)
水水月牙 2025-01-03 10:49:11

整个功能可以在函数内完成,因此它不需要是 CompulatedProperty。仅当您想要执行可能查询的计算时才使用计算属性。 CompatedProperty 可以将其 indexed 标志设置为 False 但这意味着您不会查询它,因此实际上不会需要将其作为财产。

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) 

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) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文