well...

for row in lfu:
    list_friend_updates= getfriend(row.friendid)
    print list_friend_updates
return dict(list_friend_updates=list_friend_updates)

prints all, but reassign for every loop to list_friend_updates ONLY
the getfriend(row.friendid).

You are not seeing the first record when returning the dict, but the
last one of the loop.

You need to store every result for all the records in the loop in a
list, for example

list_friend_updates_list = []
for row in lfu:
    list_friend_updates= getfriend(row.friendid)
    list_friend_updates_list.append(list_friend_updates)

return dict(list_friend_updates_list=list_friend_updates_list)

PS: sure that this can't be retrieved by joining tables instead of
retrieve recursively ?

you are doing

friendupdates = db((db.updates.reguserid == friendid ) &
(db.updates.updated_on==now)).select()

for every friendid passed by the first query

lfu = db((db.friends.reguserid == session.logged_in_user_id) &
(db.friends.status == 'Friend')).select(db.friends.friendid)

that can be accomplished by

lfu = db((db.friends.reguserid == session.logged_in_user_id) &
(db.friends.status == 'Friend'))._select(db.friends.friendid)  # mind
the ._SELECT instead of .SELECT

friendupdates = db((db.updates.reguserid.belongs(lfu) ) &
(db.updates.updated_on==now)).select()

!!!

Leaving joins to db is more efficient than doing in a recursive loop
in python!



On 6 Feb, 13:28, Rahul <[email protected]> wrote:
> Hi All,
>      I am using web2py 1.99.4 on Win7. I have this part of code that I
> want to serialize to a view but conditionally. I am trying to retrieve
> id's from one table and pass the id's to get data from another table.
> Although I am able to make it work from within the loop but from
> outside of the loop with "list_friend_updates=list_friend_updates", it
> does not work fine. It only shows me the first entry and ignores the
> rest. I think that could be a python programming problem of mine, can
> any one suggest a solution?
> I want that "list_friend_updates" should get all the rows I want to
> iterate over. I've checked other functions and they are returning
> proper values.
>
> Here is the code  from my controller -----
> def home():
> list_friend_updates=""  #Dummy value declared to stop it showing any
> issue
>
>  #Get Friends ids first
> lfu = db((db.friends.reguserid == session.logged_in_user_id) &
> (db.friends.status == 'Friend')).select(db.friends.friendid)
>
>  #pass the selected friendid recursively (using a for loop) and get
> their updates...
>
> for row in lfu:
>       #Call getfriend (see below) function below and iterate over each
> id found, getfriend function returns values
>         list_friend_updates= getfriend(row.friendid)
>         print list_friend_updates #This shows all friends records and
> works but fails if I use it in return below
>
> return dict(form=form , list_friend_updates=list_friend_updates, )
>
> ##### Get Friend Function - Get the friend id values from above
> function and iterate over each to get data #####
> def getfriend(friendid):
>       friendupdates = db((db.updates.reguserid == friendid ) &
> (db.updates.updated_on==now)).select()
>
>     return friendupdates
> ------------------------------------
> What am I doing wrong? Please suggest.
>
> Thanks,
> Rahul D (www.flockbird.com) - Build Connect beta

Reply via email to