> def av_rating(self):
> queryset = self.review_set.all()
> x=0
> y=0
> for rating in queryset:
> x=x+rating.rating
> y=y+1
> return x/y
>
> {{ objects.av_rating }} is the tag that is then put in the template.
>
> Can anybody comment on the disadvantage/advantages of using this type
> of custom model method? Also, if anybody can advise me on how I get
> the division to work - that would also be appreciated - Django returns
> a Zero Division error.
I don't know about the efficiency of calculating this, as you're
pulling back all the data just to calculate the average over the
set. I would suspect it would be faster for the server to simply
calculate the average and return it (one datum traversing the
net, rather than a whole dataset).
As for the exception/error, you could do any of a number of things:
1) return y and x/y or 0
2)
try:
return x/y
else:
return 0
3)
if y == 0:
return 0
else:
return x/y
However, having the server calculate the average should obviate
the need for such contortions.
HTH,
-tkc
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---