|
|
A generic ratings module. The field itself appends two additional fields on the model, for optimization reasons. It adds <field>_score, and <field>_votes fields, which are both integer fields.
Installation
Code is not fully functional yet.
svn checkout http://django-ratings.googlecode.com/svn/trunk/ django-ratings cd django-ratings sudo python setup.py install
You will then need to add djangoratings to your INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'djangoratings',
)Finally, run python manage.py syncdb in your appication's directory to create the tables.
Usage
from djangoratings import RatingField
RATING_CHOICES = (
(1, 'Up'),
(-1, 'Down'),
)
class MyModel(models.Model):
rating = RatingField(choices=RATING_CHOICES)Alternatively you could do something like:
from djangoratings import AnonymousRatingField
TEN_POINT_SCALE = [(n, str(n)) for n in range(1, 10)]
class MyModel(models.Model):
rating = AnonymousRatingField(choices=TEN_POINT_SCALE)Potentially, you will also be able to query on the score and votes:
MyModel.objects.filter(rating__score__gte=50).order_by('-rating__score')And adding votes is also simple:
myinstance.rating.add(score=1, user=request.user, ip_address=request.META['REMOTE_ADDR'])
Accessing information about the rating of an object is also easy:
# these do not hit the database myinstance.rating.votes myinstance.rating.score
