为 ndb 属性 覆盖默认值 _get_for_dict()
Override default _get_for_dict() for ndb Property
我很难更改默认的 _get_for_dict() 方法。
这是我的代码目前的样子:
class ImageProperty(ndb.BlobKeyProperty):
def _get_for_dict(self, entity):
value = super(ImageProperty, self)._get_for_dict(entity)
if value:
return images.get_serving_url(value)
else:
return None
我不太了解重写方法的概念,并且在使用 ndb iself 时遇到麻烦...
基本上我想做的是:将我的数据存储区密钥存储为 BlobKeyProperty,但是当将其作为字典检索时,我想获取图像服务 url.
非常感谢
我没试过,但我 认为 作为 _from_base_type
钩子会更好:
class ImageProperty(ndb.BlobKeyProperty):
def _from_base_type(self, value):
return images.get_serving_url(value)
如果我理解了documentation correctly, this API "stacks" so you don't need to call the _from_base_type
on the super class (BlobKeyProperty
). I guess ndb
handles that for you。就个人而言,我认为这对于 API 来说有点奇怪,而 super
似乎可以正常工作......但是......我猜就是这样。
我很难更改默认的 _get_for_dict() 方法。 这是我的代码目前的样子:
class ImageProperty(ndb.BlobKeyProperty):
def _get_for_dict(self, entity):
value = super(ImageProperty, self)._get_for_dict(entity)
if value:
return images.get_serving_url(value)
else:
return None
我不太了解重写方法的概念,并且在使用 ndb iself 时遇到麻烦... 基本上我想做的是:将我的数据存储区密钥存储为 BlobKeyProperty,但是当将其作为字典检索时,我想获取图像服务 url.
非常感谢
我没试过,但我 认为 作为 _from_base_type
钩子会更好:
class ImageProperty(ndb.BlobKeyProperty):
def _from_base_type(self, value):
return images.get_serving_url(value)
如果我理解了documentation correctly, this API "stacks" so you don't need to call the _from_base_type
on the super class (BlobKeyProperty
). I guess ndb
handles that for you。就个人而言,我认为这对于 API 来说有点奇怪,而 super
似乎可以正常工作......但是......我猜就是这样。