Little help needed writing models for star-ratings app

20 views
Skip to first unread message

surya

unread,
Apr 29, 2013, 12:44:01 PM4/29/13
to django...@googlegroups.com
Here is the story so far

class Rating(models.Model):
positive_rating = models.FloatField(default=0)
negative_rating = models.FloatField(default=0)

class Book(models.Model):
year_pub = models.IntegerField(max_length=4)
category = models.ForeignKey(Category)
rating = models.ForeignKey(Rating)
hash_id = models.CharField(max_length=64, null=True, blank=True)

There are few books. Each will be having 2 types of rating. +ve one and -ve one, 
The rating is done using "stars". If +ve rating is done, -ve field will be 0 vice versa.

votes could be simple stored in this way

o = Book.objects.get(pk=1)
o.rating.positive_rating += 2.5
o.rating.save()

Here comes the actual trouble -->

1. How to count the number of ratings
2. How to know which user has voted
3. How to know whether the user has previously voted or not

Typically, how to attach this MyUser (example) model to the Rating Model?

Thanks

Shawn Milochik

unread,
Apr 29, 2013, 1:01:34 PM4/29/13
to django...@googlegroups.com
It looks like you're not looking for a "little help." You're looking for someone to do the work for you.

You'll get the best help if you try something, get stuck, and explain what you tried and what the error is.

Here is the documentation for using the ORM. This is not a snarky response -- I really hope you do read it and learn how to use Django's ORM, which is what you need to answer your own questions.


Read that, try some stuff, then ask specific questions when you have problems. You'll get a lot of great help if you do that.

surya

unread,
Apr 29, 2013, 1:13:07 PM4/29/13
to django...@googlegroups.com
I am not sure but.. my concern is more about "relationships"

How should I be defining Rating model for

counting number of specific ratings it got
user who rated it.. 

probably, to log the user data

user = models.ForeignKey(User) would work

But counting number of specific ratings it got is the issue!!

Say there is a Book X. Its ratings are by default 0, 0
Now, as the users add ratings, its get added up to the default and its fine.. 

but how to know who added what ratings? and how many times he changed etc..

would a ManyToMany field with User and Rating would work?

Shawn Milochik

unread,
Apr 29, 2013, 1:41:12 PM4/29/13
to django...@googlegroups.com
I reiterate: Please read the ORM documentation. That will answer all of your questions. Once you understand how to do queries in the ORM (and which queries are easier than others) then you will know how to design your models so that they'll be easy to work with. As you're reading, make notes about how you can aggregate and search across tables/models.

Reply all
Reply to author
Forward
0 new messages