Wow, you are right. This is a tricky lookup. The generic relation
makes it pretty tough. I'd just use the python sort unless you see a
real performance problem.

class LinkGetTopScores(models.Manager):
    def get_top_score(self):
        return sorted(self.all(), key=lambda n: n.get_score)

Maybe you need to make it n.get_score(), instead of n.get_score in the
lambda.


Alex

On Feb 10, 8:54 am, Andres Lucena <andresluc...@gmail.com> wrote:
> On Thu, Feb 10, 2011 at 2:52 PM, Alex Robbins
>
> <alexander.j.robb...@gmail.com> wrote:
> > Do you want the episode with the highest individual score, or the
> > highest average score?
>
> I want the links (within that episode) sorted by highest score.
>
> (Sorry for no making it clear before)
>
> Thank you,
> Andres
>
>
>
>
>
>
>
> > Alex
>
> > On Thu, Feb 10, 2011 at 7:41 AM, Andres Lucena <andresluc...@gmail.com> 
> > wrote:
> >> On Thu, Feb 10, 2011 at 2:29 PM, Alex Robbins
> >> <alexander.j.robb...@gmail.com> wrote:
> >>> Yeah, you'll definitely want to find some aggregate to do the sorting.
>
> >> Ok, didn't know it. I'll take a look at it...
>
> >>> The only way to sort by a custom method is doing an in-python sort,
> >>> which is going to be much slower than having the db sort for you.
>
> >> Yeah, I tought so but it seems (to me) the only way of doing this...
>
> >>>  If you post the score models, we could probably help more.
>
> >> The score models are from django-voting:
>
> >>http://django-voting.googlecode.com/svn/trunk/voting/models.py
> >>http://django-voting.googlecode.com/svn/trunk/voting/managers.py
>
> >> Thanks for the help!
>
> >> Andres
>
> >>> Alex
>
> >>> On Feb 9, 8:49 am, "Casey S. Greene" <csgre...@princeton.edu> wrote:
> >>>> 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...
> >>>> > >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 
> >>> 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-users@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-users@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-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