Re: rendering to template help
hello again, i am trying to get a single entry from my db and then annotate the count and rating to it like we established above however it doesn't seem to be returning anything: videos = Video.objects.get(id=vid_id).annotate(rating_count=Count('rating'), rating_avg=Avg('rating__rating')) i even tried a filter: videos = Video.objects.all().filter(id=vid_id).annotate(rating_count=Count('rating'), rating_avg=Avg('rating__rating')) this is really puzzling me as to why it is not working. Any ideas? Also i seem to be getting an error with my |drawstars which works fine on other pages that dont require this individual selection. I don't quite understand this error: "Caught an exception while rendering: a float is required" thanks in advance On May 25, 7:58 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > On May 25, 10:30 am, Pirate Pete <peters.stay...@gmail.com> wrote: > > > sorry for this one last question, > > > do you have any idea how i would go about rounding these values and > > converting them from numbers to *'s ? > > > i tried using the round() function on the annotation however it kept > > giving me a type error "a float is required." > > > cheers > > I'd probably do a customtemplatefilter: > > @register filter > def stars(value): > return '*' * int(round(value)) > > (you need both int and round, as int always rounds down, so you need > to do the rounding before converting to int). > > Then use it like this (after loading the library in thetemplate): > {{ video.rating_avg|stars }} > -- > 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 > athttp://groups.google.com/group/django-users?hl=en. -- 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.
Re: rendering to template help
sorry for this one last question, do you have any idea how i would go about rounding these values and converting them from numbers to *'s ? i tried using the round() function on the annotation however it kept giving me a type error "a float is required." cheers On May 25, 7:10 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > On May 25, 9:52 am, Pirate Pete <peters.stay...@gmail.com> wrote: > > > Thank you very very much for your reply. > > > There is still a few things i need to grasp. > > > Firstly, why did you do this : > > annotate(rating_count=Count('rating')).annotate(rating_avg=Avg('rating__rat > > ing')) > > as opposed to annotate(rating_count=Count('rating'), > > rating_avg=Avg('rating__rating')) > > No reason, these are equivalent. > > > Secondly, why is the average rating__rating and the count is only > > rating ? > > Because you're counting the individual rating objects, but you're > averaging the values of the 'rating' field on each rating object. > > > Unfortunately the code you provided doesn't seem to be working however > > that could just be my own ignorance at work. > > > Does this annotation need to be assigned to some sort of variable and > > passed into the render or just run like you stated above? > > Yes, sorry, assign it to the 'videos' variable and pass it straight > into the template context - you can delete the rest of the view code. > -- > 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 > athttp://groups.google.com/group/django-users?hl=en. -- 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.
Re: rendering to template help
You sir have saved me many a headache, i can't honestly thank you enough All the best! On May 25, 7:10 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > On May 25, 9:52 am, Pirate Pete <peters.stay...@gmail.com> wrote: > > > Thank you very very much for your reply. > > > There is still a few things i need to grasp. > > > Firstly, why did you do this : > > annotate(rating_count=Count('rating')).annotate(rating_avg=Avg('rating__rat > > ing')) > > as opposed to annotate(rating_count=Count('rating'), > > rating_avg=Avg('rating__rating')) > > No reason, these are equivalent. > > > Secondly, why is the average rating__rating and the count is only > > rating ? > > Because you're counting the individual rating objects, but you're > averaging the values of the 'rating' field on each rating object. > > > Unfortunately the code you provided doesn't seem to be working however > > that could just be my own ignorance at work. > > > Does this annotation need to be assigned to some sort of variable and > > passed into the render or just run like you stated above? > > Yes, sorry, assign it to the 'videos' variable and pass it straight > into the template context - you can delete the rest of the view code. > -- > 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 > athttp://groups.google.com/group/django-users?hl=en. -- 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.
Re: rendering to template help
Thank you very very much for your reply. There is still a few things i need to grasp. Firstly, why did you do this : annotate(rating_count=Count('rating')).annotate(rating_avg=Avg('rating__rating')) as opposed to annotate(rating_count=Count('rating'), rating_avg=Avg('rating__rating')) Secondly, why is the average rating__rating and the count is only rating ? Unfortunately the code you provided doesn't seem to be working however that could just be my own ignorance at work. Does this annotation need to be assigned to some sort of variable and passed into the render or just run like you stated above? thanks again! On May 25, 6:08 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > 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) > > > TitleAverage Rating (Vote Count) > > {% for video in videos %} > > {{video.title}} > td>{{ratecount.0.1}}({{ratecount.0.0}}) > > {% 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 %} > {{video.title}} td>{{video.rating_avg}}({{video.rating_count}}) > {% 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 > athttp://groups.google.com/group/django-users?hl=en. -- 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.
rendering to template help
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) TitleAverage Rating (Vote Count) {% for video in videos %} {{video.title}}{{ratecount.0.1}}({{ratecount.0.0}}) {% endfor %} thanks in advance -- 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.
Re: Setting User as foreign key
Turns out i simply had to drop all the tables in the database, flush then syncdb and everything worked fine. thanks for your help :) -- 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.
Setting User as foreign key
Hello, So basically i want to link users to comments they write and ratings they give on a video rating site. this is my models.py: from django.db import models from django.contrib.auth.models import User class Video(models.Model): title = models.CharField(help_text="Title of the video.",max_length=100) url = models.URLField(help_text="URL of the video.") def __str__(self): return self.title class Rating(models.Model): user = models.ForeignKey(User, unique=True) vid = models.ForeignKey(Video, unique=True) rating = models.PositiveIntegerField(help_text="User rating of the video.") class Comment(models.Model): #user = models.ForeignKey(User, unique=True) vid = models.ForeignKey(Video, unique=True) timeposted = models.DateTimeField(help_text="Time comment was posted") comment = models.CharField(help_text="Video comment",max_length=150) what happens is i keep getting a TemplateSyntaxError when i try to edit comments/ratings in the admin area. Exception Type: TemplateSyntaxError Exception Value:Caught an exception while rendering: no such column: video_rating.user_id if anyone could point my in the right direction it would be much appreciated thanks in advance, pete -- 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.