On 10/16/06, MerMer <[EMAIL PROTECTED]> wrote: > > Jonathan, > > I don't want to the list to be filtered to only those who have a > Rating. I need to display ALL the products, while still showing the > average rating for those products which have a rating. > > Example Table: > > -------------- Price --- # Reviews ---Average Rating > Product 1 $24 2 4.5 > Product 2 $20 0 > > > Sorry if my orginal post wasn't clear enough on this point. Will your > solution still work on this basis? Many thanks. > > MerMer
In the case where a product doesn't have a rating, the product should be retrieved, with None set as its rating attribute. Again, I've only verified this on sqlite. If you want to get both the number of reviews and the average rating in the same query, instead of doing a new query for each product in order to count the number of reviews assocated with it, you could use the following: Product.objects.all().extra( select={ 'average_rating': 'SELECT AVG(appname_review.rating) FROM appname_review WHERE appname_review.product_id = appname_product.id', 'rated_reviews': 'SELECT COUNT(*) FROM appname_review WHERE appname_review.product_id = appname_product.id AND appname_review.rating IS NOT NULL', } ) In the case where there are no reviews or no reviews with scores, the rated_reviews attribute should be zero. Jonathan. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---