Re: Calculate and store the average rating

2008-12-28 Thread eldonp2

@Dave:

It is not a 1-to1 as I would like each borrower to provide his own
rating, and then compute the average ratings per book - to make
recommendations for the highest rated books.

Thanks for the example in the reply too. I will try out the SQL update
shortly.

@ Russ:
Thanks too for the input. I will read up on signals - this is news!
The denormalization - same comment as above. I'm going to try the SQL
computation first, then will look at signals.

The probem: I want to calculate the average rating in the BookRatings
model each time an individual rating of a book is captured in the Loan
model.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Calculate and store the average rating

2008-12-27 Thread Russell Keith-Magee

On Sat, Dec 27, 2008 at 7:48 AM, eldonp2 <eldo...@gmail.com> wrote:
>
> Is there a way to calculate and store the average rating in this
> example:

It's not entirely clear which part of this problem you want help with.

 - Computing the average rating. The brute force approach would be to
iterate over the relevant related objects and compute an average. A
smarter approach would be to use SQL to compute the average.

 - Storing the average in the related model. This could be as simple
as saving an instance of the BookRatings model after computing the
average. You could also use signals or save() overriding to automate
the update of the BookRatings model.

 - Model design. It's not entirely clear why you would want to
denormalize the average values, and then put them into a separate
model.

If you provide a better description of the problem you want help with,
what you have already tried, and what problems you have experienced,
we may be able to provide a more meaningful answer to your problem.

Yours,
Russ Magee %-)

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Calculate and store the average rating

2008-12-26 Thread Dave Dash

Hmm... this is slightly different... is BookRatings and Books 1 to 1?

If I store these aggregates as part of the class that it's grouping
by.  Here's how I do it for restaurants:

class RestaurantRating(models.Model):
restaurant = models.ForeignKey(Restaurant)
value  = models.IntegerField(null=True, blank=True)
user   = models.ForeignKey(Profile)
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
db_table = u'restaurant_rating'

def save(self, force_insert=False, force_update=False):
try:
super(RestaurantRating, self).save(force_insert,
force_update)
cursor  = connection.cursor()
query   = "SELECT count(value), avg(value) FROM
restaurant_rating WHERE restaurant_id = %s"
results = cursor.execute(query, (self.restaurant.id,))

for row in cursor.fetchall():
self.restaurant.num_ratings= row[0]
self.restaurant.average_rating = row[1]
self.restaurant.save()

except:
  transaction.rollback()
else:
  transaction.commit()

It's worked well for me, although I haven't used this in production
yet.  Let me know if this works for you.

On Dec 26, 4:48 pm, eldonp2 <eldo...@gmail.com> wrote:
> Is there a way to calculate and store the average rating in this
> example:
>
> RATING_CHOICES = {
>  '3' : 'Excellent',
>  '2' : 'Good',
>  '1' : 'Poor',
>
> }
>
> class Loan(
>  ...
>  book = models.ForeignKey(Book)
>  rating = models.IntegerField(choices=RATING_CHOICES)
>  ...
>
> class BookRatings(...
>  ...
>  book = models.ForeignKey(Book)
>  rating = models.IntegerField(...here I would like the average rating
> from the Loan model...)
>  nums = models.IntegerField(...and here the number of ratings
> done...)
>  ...
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Calculate and store the average rating

2008-12-26 Thread eldonp2

Is there a way to calculate and store the average rating in this
example:

RATING_CHOICES = {
 '3' : 'Excellent',
 '2' : 'Good',
 '1' : 'Poor',
}

class Loan(
 ...
 book = models.ForeignKey(Book)
 rating = models.IntegerField(choices=RATING_CHOICES)
 ...

class BookRatings(...
 ...
 book = models.ForeignKey(Book)
 rating = models.IntegerField(...here I would like the average rating
from the Loan model...)
 nums = models.IntegerField(...and here the number of ratings
done...)
 ...
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---