跳转到主要内容

一个简单、可扩展的评级系统。

项目描述

一个简单、可扩展的评级系统。

依赖项

还有一些其他的东西

也是,我一直在阅读关于《编程集体智慧》中的一些不同东西的游乐场,作者是toby segaran。

这些东西在utils.py中,如果你想要实验(或贡献!)的话,它们就在那里。

入门

如果你想要给某个模型添加评分

from django.db import models
from ratings.models import Ratings

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

    ratings = Ratings()

现在,你可以了

# 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>]

使用GFKs,FKs,随便什么

默认情况下,每次将 Ratings() 添加到您的模型时,它都会使用具有 GFK 的 RatedItem 模型。假设您只对一件事情进行评分,或者希望有显式的数据库约束 - 没有问题。您可以通过提供具有 ForeignKey 的自定义 RatedItem 模型来替代 GFK。以下是测试中的示例。

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

API 完全相同。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分发

django-simple-ratings-0.3.3.tar.gz (22.3 kB 查看哈希值)

上传时间 源代码

构建分发

django_simple_ratings-0.3.3-py2.py3-none-any.whl (19.0 kB 查看哈希值)

上传时间 Python 2 Python 3

由支持