Workaround to return a list from ComputedProperty to NDB

I am converting my application to use NDB. I used to have something like this:

@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 list generation method is a bit more complicated, I prefer to keep it that way. But when I started converting to NDB, I just replaced @db.ComputedProperty with @model.ComputedProperty , but then 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 _db_set_value there are several if / else statements that process the value according to its type, except that there is no processing for lists

Currently, it passes the first condition and throws this error when I return an empty list.

Is there any way around this and avoiding the error?

+4
source share
2 answers

All this functionality can be executed inside a function, so it does not need to be ComputedProperty . Use computed properties only when you want to perform the calculations you can request. A ComputedProperty may have the indexed flag set to False , but then this means that you will not request it, and therefore do not have 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) 
+2
source

You need to set the repeat = True flag for your computed property in NDB. I don’t think you can use the pretty note "@ db.ComputedProperty", you need to say:

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

Source: https://habr.com/ru/post/1391423/


All Articles