Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
Created January 14, 2011 15:59
Show Gist options
  • Select an option

  • Save ericmoritz/779788 to your computer and use it in GitHub Desktop.

Select an option

Save ericmoritz/779788 to your computer and use it in GitHub Desktop.
I really simple, stupid MixIn to enable r/w of micromodels in Riak
class RiakModelMixIn(object):
"""Simply define a property "key" that generates a key
based on the model's fields and you can store them in riak
Example::
class MyModel(micromodels.Model):
slug = micromodels.CharField()
title = micromodels.CharField()
@property
def key(self):
return self.slug
"""
@classmethod
def get_bucket(cls):
bucketname = cls.__name__.lower()
return client.bucket("%s.%s" % (PREFIX, bucketname))
@classmethod
def load(cls, key):
bucket = cls.get_bucket()
obj = bucket.get(key)
return cls(obj.get_data())
def save(self):
bucket = self.get_bucket()
data = self.to_dict(serial=True)
log.debug("Storing %s in %s" % (self.key, bucket.get_name()))
obj = bucket.new(self.key, data=data)
obj.store()
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment