I haven't used django-voting but it sounds to me like you want something
like:
Link.objects.aggregate(Avg(score = 'vote__score')).order_by('score')

If I recall correctly you can chain aggregate and order_by.

Anyway, that example and this link should get you started at least:
http://docs.djangoproject.com/en/dev/topics/db/aggregation/

Hope this helps!
Casey

On Wed, 2011-02-09 at 10:08 +0100, Andres Lucena wrote:
> On Tue, Feb 8, 2011 at 6:00 PM, Andres Lucena <andresluc...@gmail.com> wrote:
> > Dear Gurus,
> >
> > I've made a custom method for getting the score (from django-voting)
> > for a giving Model:
> >
> > class Link(models.Model):
> >    episode = models.ForeignKey("Episode", related_name="links")
> >    url = models.CharField(max_length=255, unique=True, db_index=True)
> >
> >    def __unicode__(self):
> >        return self.url
> >
> >    def get_score(self):
> >        return Vote.objects.get_score(self)['score']
> >
> > Now I want to make a custom manager to getting the top-scored links
> > for the given episode. AFAIK, you can't sort by a custom method, so
> > I'm trying to apply the ordering through sorted(), like this links
> > says:
> >
> > http://stackoverflow.com/questions/981375/using-a-django-custom-model-method-property-in-order-by
> > http://stackoverflow.com/questions/883575/custom-ordering-in-django
> >
> > So, what I have now is this:
> >
> > class LinkGetTopScores(models.Manager):
> >    def get_top_score(self):
> >        return sorted(self.filter(episode=self.episode), key=lambda n:
> > n.get_score)
> >
> > class Link(models.Model):
> >    episode = models.ForeignKey("Episode", related_name="links")
> >    url = models.CharField(max_length=255, unique=True, db_index=True)
> >    get_top_score = LinkGetTopScores()
> > ....
> >
> > So of course this isn't working because of the self.episode stuff...
> > But I've to filter somehow by episode (the ForeignKey), and I don't
> > know how. Is there anyway of doing this?? What I'm doing is right or
> > there would be an easier way of doing this?
> >
> 
> I noticed that the .filter isn't necesary, so now I have this:
> 
> class LinkGetTopScores(models.Manager):
>     def get_top_score(self):
>         return sorted(self.all(), key=lambda n: n.get_score)
> 
> But it don't sort by score, and I don't know what I'm doing wrong :S
> 
> Any idea?
> 
> Thanks,
> Andres
> 
> 
> > Thank you,
> > Andres
> >
> 

-- 
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.

Reply via email to