Having used multiple ORMs in the past, Peewee and ActiveRecord have struck me as my favorites. In this post, I will show the small amount of code necessary to create a ZipJSONField for personal use.

The ZipJSONField stores JSON data as zipped blobs in the database and transparently marshals the data from python and the DB.

import json
import zlib

class ZipJSONField(JSONField):
    def db_value(self, value):
        if value is not None:
            value = buffer(zlib.compress(json.dumps(value)))

        return value

    def python_value(self, value):
        try:
            value = zlib.decompress(value)
        except:
            pass

        return value if value is None else json.loads(value)

I know this is a small amount of code, but that is what I like about Peewee. Having done the same work in Sequelize.js, I am happy with how simple this solution is in python.