On Fri, 2008-01-04 at 03:26 +0000, Tom Badran wrote:
> Sure malcolm. Essentially what i have is table of pictures defined by
> the following model (forgive typos, this is just extracted minus extra
> junk. Album class is a model with title fields etc.):
> 
> class Picture(models.Model ):
>     album = models.ForeignKey(Album)
>     image = models.ImageField(upload_to="some_sane_directory")
> 
>     class Meta:
>         ordering = ['id']
> 
> Then in my view i get a picture by id using: 
> 
> picture = get_object_or_404(Picture, id=id) // where id is the
> parameter to the view function
> 
> And i want to be able to do something along the lines of (but with
> minimal amount of database stress):
> 
> album_pictures = Picture.objects.filter(album=picture.album)
> total = album_pictures.count()
> index = album_pictures.index_of(picture)

Okay, so you have to do this boring way and just pull things back into
Python structures. You want to used .index(), so convert from an
iterator to a list and you'll be able to do that:

        index = list(album_pictures).index(picture)
        
This works because two Model instances compare as "equal" when their
primary keys are equal. So although album_pictures won't contain exactly
the same Python object (in the sense that id() will return different
values), it will contain something with the same pk as picture and that
is all that's required.

Regards,
Malcolm


-- 
He who laughs last thinks slowest. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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