Skip to main content

a simple, extensible rating system.

Project description

a simple, extensible rating system.

dependencies:

With a little bit more

also, a playground for different stuff i’ve been reading about in programming collective intelligence, by toby segaran.

this stuff lives in utils.py and is there if you want to experiment (or contribute!).

Getting started

you’d like to add ratings to some model:

from django.db import models
from rating.models import Ratings

class Food(models.Model):
    name = models.CharField(max_length=50)

    ratings = Ratings()

now, you can:

# add ratings to things
>>> apple.ratings.rate(user=john, score=1)
<RatedItem: apple rated 1 by john>

>>> apple.ratings.rate(user=jane, score=5)
<RatedItem: apple rated 5 by jane>

# get interesting aggregate data
>>> apple.ratings.all()
[<RatedItem: apple rated 1 by john>, <RatedItem: apple rated 5 by jane>]

>>> apple.ratings.cumulative_score()
6

>>> apple.ratings.average_score()
3.0

# order things by their rating
>>> Food.ratings.order_by_rating()
[<Food: apple>, <Food: orange>]

Use GFKs, FKs, whatever

By default, whenever you add Ratings() to your model it uses the RatedItem model which has a GFK on it. Suppose you are only rating one thing, or would like to have an explicit database constraint – that’s no problem. You can provide a custom RatedItem model with a ForeignKey instead of a GFK. Here’s the example from the tests:

class BeverageRating(RatedItemBase):
    content_object = models.ForeignKey('Beverage')


class Beverage(models.Model):
    name = models.CharField(max_length=50)

    ratings = Ratings(BeverageRating)

    def __unicode__(self):
        return self.name

The API is exactly the same.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-simple-ratings-0.3.2.tar.gz (14.4 kB view details)

Uploaded Source

File details

Details for the file django-simple-ratings-0.3.2.tar.gz.

File metadata

File hashes

Hashes for django-simple-ratings-0.3.2.tar.gz
Algorithm Hash digest
SHA256 5a99aab9f4ee37c5a2101c2829ae5662a5e705672aff93c7ad5c32b4edc40208
MD5 ea477e02a537dd9ba0a33f24c3f2d437
BLAKE2b-256 589479614b97fbcdd45dc94787760b0c41470208ae93952940160e78dbaa3c53

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page