<snip>
> > class Photo(models.Model):
> >     title = models.CharField(maxlength=50)
> >     rating = models.PositiveIntegerField(default=0)
> > 
> > class Album(models.Model):
> >     title = models.CharField(maxlength=50)
> >     pub_date = models.DateField()
> > 
> > class AlbumPhoto(models.Model):
> >     album = models.ForeignKey(Album, related_name='items')
> >     photo = models.ForeignKey(Photo)
> >     position = models.PositiveIntegerField()
<snip>

> What filter construction are you trying to do here (you've just posted
> the order_by(...) part, but are you querying the Album model or the
> AlbumPhoto model)? Can you post the SQL we are constructing ("from
> django.db import connection" and then have a look at
> connection.queries[-1]['sql']).

Okay, basically I want the photos with the highest rating for the album.
I've messed around with a few ways of doing it, but at the moment I have
something like:

models.py:

class AlbumPhotoManager(models.Manager):
    def top_rated(self):
        return self.order_by('-photo__rating')

class AlbumPhoto(models.Model):
    album = models.ForeignKey(Album, related_name='items',
edit_inline=True, raw_id_admin=True)
    photo = models.ForeignKey(Photo)
    position = models.PositiveIntegerField(core=True)
    objects = AlbumPhotoManager()

template:

{% for item in album.items.top_rated %}
  <img src="{{ item.photo.get_image_url }}" /></a>
{% endfor %}


It's giving me an exception:
ERROR: column albums_albumphoto.photo__rating does not exist
SELECT
"albums_albumphoto"."id","albums_albumphoto"."album_id","albums_albumphoto"."photo_id","albums_albumphoto"."position"
 FROM "albums_albumphoto" WHERE ("albums_albumphoto"."album_id" = 1) ORDER BY 
"albums_albumphoto"."photo__rating" DESC

Obviously I can't order by photo__rating like I was thinking. I'm
getting the feeling I'm putting something in the wrong place. Should I
be using managers like this?

I'll keep plugging away.

Cheers again,

Nick


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

Reply via email to