-
-
Save skyl/412750 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']> |
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?
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
(To be clear, you could make Counter itself the descriptor if you want to, as long as it also has the contribute_to_class method so you get the attname. But you could also make some other object be the descriptor; depends what makes for nicer code).