class Restaurant(models.Model)
food_rating = RatingField(range=2, weight=5,can_change_vote = True,allow_delete = True,allow_anonymous = True)
service_rating = RatingField(range=2, weight=5,can_change_vote = True,allow_delete = True,allow_anonymous = True)
ambience_ratiing = RatingField(range=2, weight=5,can_change_vote = True,allow_delete = True,allow_anonymous = True)
r = Restaurant.objects.get(pk=1)
r.food_rating.add(score = -1 , user = request.user , ip_address =request.META.get('HTTP_REFERER'))
print r.food_rating.score
djangoratings.exceptions.InvalidRating: -1 is not a valid choice for food_rating
我的food_rating字段有资格获得两个分数,我应该如何更改分数以便我可以实施投票和投票功能,在投票时,我应该能够在现有分数上加1投票我可以减去投票,请提前帮助,谢谢。
答案 0 :(得分:1)
问题来自this script:
if score < 0 or score > self.field.range:
raise InvalidRating("%s is not a valid choice for %s" % (score, self.field.name))
简答:将您要用于显示的[-x:y]间隔转换为代码中的[-x + x:y + x]以避免此问题。如果你想要[-5:5],那么使用[-5 + 5:5 + 5],这是[0:10]。如果你想要[-50:100]然后使用[-50 + 50:100 + 50] = [0:150]等等......这是一个简单的公式,对于程序员来说应该不是问题;)
答案很长:要么是你的djangoratings,要么你打开一个问题,要求添加一个启用负评级的设置......可能他会拒绝它,因为简单的间隔转换解决方法,这里有一些更具体的例子:< / p>
class Restaurant(models.Model):
# blabla :)
ambience_rating = RatingField(range=5, weight=5,can_change_vote = True,allow_delete = True,allow_anonymous = True)
def get_adjusted_ambiance_rating(self):
return self.ambience_rating - 3
因此,如果ambience_rating为“1”(最低分),则get_adjusted_ambiance_rating()将返回-2。
如果ambience_rating为“5”(最高分),则get_ambiance_rating_with_negative()将返回2.
根据您的需要调整此示例/技巧。
您应该为所有评级制定一个方法:
def get_adjusted_rating(self, which):
return getattr(self, '%s_rating' % which) - 3
可以这样调用:
restaurant.get_adjusted_rating('ambiance')
restaurant.get_adjusted_rating('food')
# etc ...
也许是模板过滤器:
@register.filter
def get_adjusted_rating(restaurant, which):
return restaurant.get_adjusted_rating(which)
可以这样使用:
{{ restaurant|get_adjusted_rating:"ambiance" }}
{{ restaurant|get_adjusted_rating:"food" }}
{# etc, etc #}