Re: QuerySets - Reverse Look-Up for Index
Although it works in my interactive environment.. it does not work in my actual script.. ?? On Mar 9, 7:02 pm, Calagannewrote: > Hey, > > thanx for the reply! No, the bid is *not* ordered randomly, but you > know what, your solution works anyway! ;) Test this and you'll notice > that it works.. (still haven't figured out why!): > > * bs = BID.objects.all () > * idx=81; bid=bs[idx]; > * bs.order_by ('?').filter (pk__lt=bid.id).count () ## == idx > > Can u explain this? I've checked even for *every* bid in bs if this > condition holds for my sample, and it was true. My test routine was: > > * for idx, bid in enumerate(bs): > * idx, bs.order_by ('?').filter (pk__lt=bid.id).count () - idx == > 0 > > .. strange. > > On Mar 7, 11:50 am, greatlemer wrote: > > > if bids is ordered by index then you could just return > > BID.objects.filter(...).filter(pk__lte=id).count() and that should be > > what your after in one step (if I've understood the question > > correctly). > > > -- > > G > > > On Mar 7, 7:10 am, Hasan Karahan wrote: > > > > Hi, > > > > I'm wondering if there is an efficient way to look-up the index of a given > > > object in a query-set? I'm using now the following approach: > > > > def get_index (id): > > > > bids = BID.objects.filter (..) > > > obj2idx = dict (zip (bids, xrange (bids.count ())) > > > > bid = BID.objects.get (id=ID) > > > > return obj2idx (bid) > > > > This has a linear time processing.. is there a more efficient way to do > > > it? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: QuerySets - Reverse Look-Up for Index
Hey, thanx for the reply! No, the bid is *not* ordered randomly, but you know what, your solution works anyway! ;) Test this and you'll notice that it works.. (still haven't figured out why!): * bs = BID.objects.all () * idx=81; bid=bs[idx]; * bs.order_by ('?').filter (pk__lt=bid.id).count () ## == idx Can u explain this? I've checked even for *every* bid in bs if this condition holds for my sample, and it was true. My test routine was: * for idx, bid in enumerate(bs): * idx, bs.order_by ('?').filter (pk__lt=bid.id).count () - idx == 0 .. strange. On Mar 7, 11:50 am, greatlemerwrote: > if bids is ordered by index then you could just return > BID.objects.filter(...).filter(pk__lte=id).count() and that should be > what your after in one step (if I've understood the question > correctly). > > -- > G > > On Mar 7, 7:10 am, Hasan Karahan wrote: > > > Hi, > > > I'm wondering if there is an efficient way to look-up the index of a given > > object in a query-set? I'm using now the following approach: > > > def get_index (id): > > > bids = BID.objects.filter (..) > > obj2idx = dict (zip (bids, xrange (bids.count ())) > > > bid = BID.objects.get (id=ID) > > > return obj2idx (bid) > > > This has a linear time processing.. is there a more efficient way to do it? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.
Re: QuerySets - Reverse Look-Up for Index
if bids is ordered by index then you could just return BID.objects.filter(...).filter(pk__lte=id).count() and that should be what your after in one step (if I've understood the question correctly). -- G On Mar 7, 7:10 am, Hasan Karahanwrote: > Hi, > > I'm wondering if there is an efficient way to look-up the index of a given > object in a query-set? I'm using now the following approach: > > def get_index (id): > > bids = BID.objects.filter (..) > obj2idx = dict (zip (bids, xrange (bids.count ())) > > bid = BID.objects.get (id=ID) > > return obj2idx (bid) > > This has a linear time processing.. is there a more efficient way to do it? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.