Code Monkey home page Code Monkey logo

django-simple-ratings's Introduction

django-simple-ratings

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 ratings.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.

django-simple-ratings's People

Contributors

coleifer avatar kronok avatar zerok avatar jezdez avatar ryankask avatar mikelopez avatar nicolaselie avatar fabiosantoscode avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.