couchdbkit icon indicating copy to clipboard operation
couchdbkit copied to clipboard

Allow types contained by documents to serialize themselves

Open ahall opened this issue 14 years ago • 1 comments

class MyDoc(Document): animals = DictProperty()

class Animal(object): def init(): self.name = "Joe" self.age = 14

def to_json(): """Code to serialize to json""" def from_json: """Code to load from json""" ...

The animal class intentional is not a couchdb model and we don't want to tie it to couchdb. What we want is being able to have it automatically serialized and deserialized by couchdbkit calling these methods it implements. In this case the contract would be to_json and from_json.

doc = MyDoc() doc.animals['horsea'] = Animal(...) doc.animals['horseb'] = Animal(...) doc.save()

Currently this will raise an exception saying that Animal is not in ALLOWED_TYPES.

ahall avatar Jun 22 '10 07:06 ahall

A good example of how to do this would be to look at python-couchdb: http://code.google.com/p/couchdb-python/source/browse/couchdb/mapping.py

The properties themselves should describe how to get, set, serialize, and deserialize. This pattern is closer to Django's own pattern as well.

zbyte64 avatar Jul 28 '11 23:07 zbyte64