Okay I'm gone crazy over the last couple days trying to figure this
out.

I'm creating a movie ranking application. To show the movies I create
a QuerySet with all the movies in the "group" the users is looking at.
Then I pull all the votes that group has given to that movie and get
the average. I give each movie in the movie set a new attribute name
score. I now want to sort that QuerySet with the new 'score' attribute
that i've calculated.

I've tried the movies.sort('score') and movies.order_by('score')
neither work.

I'm going crazy with this problem. Anyone know how to sort a QuerySet
with an attribute that's not part of the database?? Transform it into
l list? Is there a different sort function?

If you're interested here's my code I'm using:

Thanks,
Poz


def event(request, event_id):
    try:
        e = Event.objects.get(id__exact=event_id)
    except Event.DoesNotExist:
        raise Http404
    movies = e.movies.all()  <-------------------------------- HERE'S
WHERE THE QUERYSET IS CREATED
    user = request.user
    for movie in movies:
        votes = Vote.objects.filter(event=e, movie=movie)
        totalscore = 0
        totalvoters = 0
        for vote in votes:
            totalscore += vote.score
            totalvoters += 1
        if totalscore == 0:
                movie.score = 0
        else:
            movie.score = float(totalscore) / totalvoters
<-------------------------------- HERE'S WHERE I ADD THE SCORE
ATTRIBUTE TO THE QUERYSET
            movie.totalscore = totalscore
            movie.totalvoters = totalvoters
        if Vote.objects.filter(event=e, user=user, movie=movie):
            uservote = Vote.objects.get(event=e, user=user,
movie=movie)
            movie.uservote = uservote.score
        else:
            movie.uservote = 0
    votes = Vote.objects.filter(event=e, user=user)
    form = MovieForm()
    movies = movies.order_by('sort') <--------------------------------
HERE'S WHERE I WANT TO SORT THE QUERYSET
    return render_to_response('event.html', {'event': e, 'movies':
movies, 'user': user, 'score': totalscore, 'form': form,
'searchresults': searchresults})


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to