Josyula Krishna
Josyula Krishna

Reputation: 1095

Web2Py insert record from list

This is my table

db.define_table(
'user_interests', 
Field('user_id',db.auth_user),
Field('Science','boolean'),
Field("Arts",'boolean'),
Field("BusinessEconomy",'boolean'),
Field("ComputersTechnology",'boolean')
)

and a list which has boolean values in it each corresponding to the value of the field in the table

interests = [True, False, True, True]

Is there a way to insert this list directly into the table like

db.user_interests.insert(user_id = auth.user_id, interests) 

How can i do it? Any help is appreciated.

Upvotes: 1

Views: 1095

Answers (1)

espern
espern

Reputation: 659

I think you could try something like :

lib_interests = ["Science", "Arts", "BusinessEconomy", "ComputersTechnology"]
interests = [True, False, True, True]
data = dict(zip(lib_interests, interests))
data.update(user_id = auth.user_id)
db.user_interests.insert(**data) 

Upvotes: 3

Related Questions