On May 25, 6:09 am, Pirate Pete <peters.stay...@gmail.com> wrote:
> I have been trying to get some kind of kind response from django for a
> few days now and it has been very unkind to me :P
>
> basically i need to create a page that has a video title (link) and an
> average rating with the amount of people that rated it in brackets.
>
> so it will look like ... Awesom video 5(3)  : where awesom video is
> the name 5 is the average rating and 3 people have rated the video.
>
> i can display the videos easily by using a for loop in the template
> however i am having much difficulty displaying the average ratings and
> count
>
> rating model:
>
> class RatingManager(models.Manager):
>         def average_rating(self,video):
>                 avg = 
> Rating.objects.filter(id=video.id).aggregate(Avg('rating'))
>                 return avg
>
>         def vote_count(self,video):
>                 num = 
> Rating.objects.filter(id=video.id).aggregate(Count('rating'))
>                 return num
>
> class Rating(models.Model):
>         user = models.ForeignKey(User, unique=False)
>         vid = models.ForeignKey(Video, unique=False)
>         rating = models.PositiveIntegerField(help_text="User rating of the
> video.")
>         objects = RatingManager()
>
>         def __str__(self):
>                 return str(self.user) + "'s rating: " + str(self.vid)
>
> View:
>
> def index(request):
>         videos = Video.objects.all()
>         dict= []
>         for vid in videos:
>                 ratec =
> (Rating.objects.vote_count(vid),Rating.objects.average_rating(vid))
>                 dict.append(ratec)
>
>         return render_to_response("index.html",
> {'videos':videos,'ratecount':dict})
>
> index.html: (please note ratecount is just trial and error this could
> potentially be by all means completely incorrect)
>
> <table><tr><th>Title</th><th>Average Rating (Vote Count)</th></tr>
>         {% for video in videos %}
>         <tr><td><a href="{{video.url}}">{{video.title}}</a></
> td><td>{{ratecount.0.1}}({{ratecount.0.0}})</td>
>         {% endfor %}
>
> thanks in advance
>

Firstly, please don't call a variable 'dict' - it shadows Python's
built-in dict type. Especially not if the variable is actually a list,
not a dict.

Secondly, you're just showing the same rate count - the first one - on
each and every video. I'm sure this isn't what you want.

I think what you actually want to do here is to annotate the count and
avg on the video, not calculate separate aggregations for each one.
You can do this directly in the view, something like:

 
Video.objects.all().annotate(rating_count=Count('rating')).annotate(rating_avg=Avg('rating__rating'))

Then in the template you can just do:

        {% for video in videos %}
        <tr><td><a href="{{video.url}}">{{video.title}}</a></
td><td>{{video.rating_avg}}({{video.rating_count}})</td>
        {% endfor %}

--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.

Reply via email to