The relevant documentation btw:

http://docs.djangoproject.com/en/dev/topics/db/aggregation/
http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

On Jul 20, 10:18 am, Andrew Fong <fongand...@gmail.com> wrote:
> Assuming your models are like this:
>
> class Book(models.Model):
>     name = models.CharField(max_length=128)
>
> class User(models.Model):
>     is_staff = models.BooleanField(default=False)
>
> class Rating(models.Model):
>     user = models.ForeignKey(User)
>     score = models.IntegerField(default=3)
>     book = models.ForeignKey(Book)
>
> Try this:
>
> from django.db.models import Sum
> Book.objects.filter(rating__user__is_staff=True).annotate(score=Sum
> ('rating__score')).order_by('-score')[0:10]
>
> -- Andrew
>
> On Jul 20, 9:22 am, The Danny Bos <danny...@gmail.com> wrote:
>
>
>
> > Hey there,
>
> > I'm looking at getting a Top 10 of all Books on a site, but only where
> > rated by users of a certain Group.
> > Here's what I've got so far:
>
> >         book = Book.objects.all()
>
> > Somewhat impressive, hey?
>
> >  - So, my tables/models are Book, Rating, User.
> >  - I save all ratings in Rating like so "rating | user | book"
> >  - I'd just like the Top 10 as rated by users in the group "Staff".
>
> > Hope that helps,
> > I'm really stuck on how to get this moving.
>
> > d
--~--~---------~--~----~------------~-------~--~----~
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