Skip to content Skip to sidebar Skip to footer

How To Subclass Google App Engine Ndb Property To Support Python Subclassed Objects

from this article https://stackoverflow.com/a/32107024/5258689 I have a dict() subclass - that allows me to do dict.key (use dot to access keys i mean) - as follows: class Permissi

Solution 1:

App Engine's ndb package doesn't support saving dictionaries directly, but json can be saved in a JsonProperty, and dictionaries are easily encoded as json, so the simplest implementation is a subclass of JsonProperty that returns a Permissions instance when accessed.

class PermissionsProperty(ndb.JsonProperty):

    def _to_base_type(self, value):
        return dict(value)

    def _from_base_type(self, value):
        return Permissions(value)

This implementation is incomplete though, because JsonProperty will accept values that aren't Permissions instances, so you need to add a _validate method to ensure that what you're saving is the right type of object.

class PermissionsProperty(ndb.JsonProperty):

    def _to_base_type(self, value):
        return dict(value)

    def _from_base_type(self, value):
        return Permissions(value)

    def _validate(self, value):
        if not isinstance(value, Permissions):
            raise TypeError('Expected Permissions instance, got %r', % value)

Solution 2:

as @snakecharmerb said, it works with JasonProperty and also as another solution i found is here ObjectProperty()

that supports any python object by pickle it and save it to datastore and reverse!

and here's my code after:

class Permissions(dict):
def __init__(self, *args, **kwargs):
    super(Permissions, self).__init__(*args, **kwargs)
    for arg in args:
        if isinstance(arg, dict):
            for k, v in arg.iteritems():
                self[k] = v

    if kwargs:
        for k, v in kwargs.iteritems():
            self[k] = v

def __getstate__(self):
    return self.__dict__

def __setstate__(self, d):
    self.__dict__.update(d)

def __getattr__(self, attr):
    return self.get(attr)

def __setattr__(self, key, value):
    self.__setitem__(key, value)

def __setitem__(self, key, value):
    super(Permissions, self).__setitem__(key, value)
    self.__dict__.update({key: value})

def __delattr__(self, item):
    self.__delitem__(item)

def __delitem__(self, key):
    super(Permissions, self).__delitem__(key)
    del self.__dict__[key]


class PermissionsProperty(ndb.PickleProperty):

    def _to_base_type(self, value):
        return pickle.dumps(value)

    def _from_base_type(self, value):
        return pickle.loads(value)

    def _validate(self, value):
        if not isinstance(value, Permissions):
            raise TypeError('Expected Permissions instance, got %r' % value)

note that I added getstate() and setstate() in order for pickling correctily other wise it gives a pickling error.


Post a Comment for "How To Subclass Google App Engine Ndb Property To Support Python Subclassed Objects"