Skip to content

Instantly share code, notes, and snippets.

@skyl
Forked from djfroofy/djredis_iface.py
Created May 25, 2010 04:09
Show Gist options
  • Save skyl/412750 to your computer and use it in GitHub Desktop.
Save skyl/412750 to your computer and use it in GitHub Desktop.
from djredis.models import DredisMixin
import djredis.models
class Blog(models.Model, DredisMixin): # inherit from the mixin class
author = models.ForeignKey('Author')
title = models.CharField(max_length=200)
# declaratively add your redis fields
viewcount = djredis.models.Counter()
myzset = djredis.models.Zset()
>>> blog = Blog.objects.get(pk=1)
>>> blog.viewcount.incr()
1
>>> blog.viewcount.incr(5)
6
>>> blog.myzset
<SortedSet: []>
>>> blog.myzset.add('foo', 10)
True
>>> blog.myzset
<SortedSet: ['foo']>
@skyl
Copy link
Author

skyl commented May 26, 2010

This is now implemented in my descriptor branch, but what about the cls/table - level fields? What would the usage of the api look like?

@carljm
Copy link

carljm commented May 26, 2010

Should work pretty much the same, right? Except you'd access it via the class object rather than an instance.

If certain attributes only make sense to use at the class level, or vice versa, it's pretty easy to check for that in a descriptor (since get gets both instance and class args) and error out if it's being called the wrong way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment