开发者

Django ratings system

I'm working on a project that needs a ratings system where they can rate the quality of the houses. They'd be doing it from the admin panel, so it's not something that a visitor would rate.

For example, for each listing, they can rate it from 1-5开发者_开发百科 stars: Location: Room: Dining options: Community: Overall:

In my models.py file, I have this setup:

Class Listing (models.Model):

...

    RATING_CHOICES = (
        (1, 1),
        (2, 2),
        (3, 3),
        (4, 4),
        (5, 5),
    )
    location_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
    room_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
    dining_options_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
    community_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
    recreation_rooms_rating = models.DecimalField(choices = RATING_CHOICES, max_digits=3, decimal_places=2)
    def __unicode__(self):
        return self.name

    def get_avg_rating(self):
        avg_rating = (self.location_rating + self.room_rating + self.recreation_rooms_rating + self.dining_options_rating + self.community_rating) / 5
        return avg_rating

I'd plan to display the ratings with a little CSS. Just putting room_rating or avg_rating in a template tag doesn't work, so I'm assuming I'd have to add some lines of code to views.py. I've checked the Django docs, but I'm still not sure how to go about this.

This seems like something that should be easy to do, but I'm not really sure where to start.


A potential problem with the direction you taking is that you're only storing one set of ratings for each Listing. If user B wants to rate a Listing, he'll have to overwrite User A's ratings. Instead of putting the ratings inside the Listing, you'll need to break out your ratings into a separate Ratings table, and use a ManytoMany relationship between your Listings and Ratings. That way, you can have multiple users rate a particular listing, and then you can calculate and show the averages for each listing. See this discussion for reference.

It might be better to use an existing ratings app than starting from scratch. They already have documentation so you can get them up and running quickly, and they handle the database design for you.

Django Ratings

Django Valuate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜