简体   繁体   中英

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.

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM