Thanks Anthony, i understand clearly now, getcourseids was indeed a Rows 
object and it works now with just  this line:
s = db(db.item.id.belongs([c.item_id for c in getcourseids])).select(db.item
.course_title)

Thanks a lot really!

On Wednesday, September 21, 2016 at 8:59:05 PM UTC+2, Anthony wrote:
>
> On Wednesday, September 21, 2016 at 3:30:49 AM UTC-4, Meinolf wrote:
>>
>> OMG, Is this normal?????
>> i got chills telling me it had something to do with the *For Loop*, 
>> played around with the LBs and UBs and the following worked:
>>
>>        * for i in range(-1, (len(getcourseids)-1)):*
>>
>
> First, if you want the subscripts of a list, just do range(len(thelist)). 
> Do not subtract 1 from the length of the list -- range already handles that 
> for you (it returns integers that are strictly less than the second 
> argument). Also, no need to start with 0, as that is the default. And don't 
> start with -1 -- when used as a subscript, that will simply retrieve the 
> *last* item in the list, not the first. Also, in Python 2, use xrange() 
> in for loops, which is more memory efficient (in Python 3, range has been 
> replaced by xrange).
>  
>
>>            * c = db.item.id <http://db.item.id>==getcourseids[i].item_id*
>> *            s.append(db(c).select(db.item.course_title)[i])*
>>
>
> Here it is not clear why you are subscripting the result of the select 
> with [i]. Presumably each query returns a single matching row, which means 
> you want the subscript to always be [0] -- otherwise, you will get a list 
> index out of range error.
>
> Actually, given that you don't really need the index of each element in 
> the list, you can instead just iterate over the list itself:
>
> for item in getcourseids:
>
> And if you did need the index values in addition to the elements, the more 
> typical approach in Python would be:
>
> for i, item in enumerate(getcourseids):
>
> Anyway, in this case you should not be using the above method at all, as 
> it is very inefficient (you are doing a separate database query for every 
> single item in getcourseids, when you could instead use a single query). 
> Villas has the right idea, but getcourseids.values() assumes getcourseids 
> is a dictionary -- presumably it is actually a Rows object. So, you can do:
>
> s = db(db.item.id.belongs([c.item_id for c in 
> getcourseids])).select(db.item.course_title)
>
> Note, the above is a Rows object. You can convert it to a list if you 
> really need a list, but most likely you can work with the Rows object (it 
> can be iterated and indexed just like a list).
>
> Assuming getcourseids is a Rows object and the only reason it was created 
> was to get the item_id values to be used in this subsequent query, you can 
> instead skip the creation of getcourseids and just use a nested select 
> <http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#belongs>
> :
>
> id_query = db([your query to retrieve course 
> ids])._select(db.your_course_table.item_id)
> s = db(db.item.id.belongs(id_query)).select(db.item.course_title)
>
>
> Anthony
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to