lkshrk lkshrk - 1 year ago 64
Python Question

Override default _get_for_dict() for ndb Property

i'm having a hard time changing the default _get_for_dict() Method.
This is what my code looks at the moment:

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)
return None

I'm not that much into the concepts for overriding methods, and having trouble with ndb iself...
Basically what I want to do: Store my Datastore Key as a BlobKeyProperty, but when retrieving it as a dict I want to get the image serving url.

Thanks a lot

Answer Source

I haven't tried this, but I think that this would be better as a _from_base_type hook:

class ImageProperty(ndb.BlobKeyProperty):
    def _from_base_type(self, value):
        return images.get_serving_url(value)

If I understand the 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. Personally, I think this is a bit weird for an API when super seems like it would work just fine ... But ... that's how it is I guess.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download