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)

It's possible this is a completely batshit crazy way of getting to where i
want to be, so let me know if i've completely missed the obvious way to do
this.

Thanks

Tom

On Jan 4, 2008 1:37 AM, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:

>
>
> On Thu, 2008-01-03 at 15:20 +0000, Tom Badran wrote:
> > Thanks for the hints Tim. The problem is that i'm not using the whole
> > sequence, im just pulling one item out of the query set and was hoping
> > for a way to get the position without having to use the whole set, in
> > the same vain that count() is much more sensible a choice than len()
> > as it gets optimised down to an SQL count by django. Thanks for
> > pointing out enumerate though, im actually embarrassed i didn't know
> > that, it looks very very useful.
>
> The problem is that you're asking for the equivalent of "given an object
> x that came from a list L at some random point in the past, what is the
> index of x in L?" Note that the answer is NOT necessarily L.index(x) for
> Python lists, because that only finds the first occurrence of "x" (and
> it's not L.index(x) for all iterable sequence, since the index concept
> isn't necessarily determinable for a non-reversible iterator). What I'm
> trying to say is that the general case is not solvable here. You need to
> know how you got the original object to know where it came from. So it's
> very much going to depend on how you pulled out the object 'x'.
>
> Do you have some short code that demonstrates how you are extracting the
> object in the first place? That might help with suggestions as to how
> you can note the position later.
>
> Regards,
> Malcolm
>
> --
> Why be difficult when, with a little bit of effort, you could be
> impossible.
> http://www.pointy-stick.com/blog/
>
>
> >
>


-- 
Tom Badran
http://badrunner.net

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