Is there a way to handle table name and field name at run time ?
I have found references to an insert method that takes a dict as argument But assuming rd is a dict with 10 entries, key = name of the field, value = value to be inserted, the following code will fail
db.OrdersFull.insert(rd)
With error :TypeError: insert() takes exactly 1 argument (2 given)
1)any clue?
2)with this solution, the table name (OrdersFull) must still be known at development time, any way to insert into a table whose name is known at run time only ?
3)Is there a place where one can found reference information for available web2py API calls ?
答案 0 :(得分:0)
要访问基于产生其名称的Python表达式的表,您可以执行以下操作:
tablename = 'mytable'
db[tablename]
已记录在here中。
要将字典与.insert()
方法一起使用,可以使用标准的Python关键字参数解包:
db[tablename].insert(**mydict)
这不是特定于web2py或DAL,而是标准的Python语法。
还有一种特殊的方法可用于过滤字典中不是表中字段的任何项目:
db.mytable.insert(**db.mytable._filter_fields(mydict))
如果要传递先前从表中选择的记录,则上述内容很有用,因为它会自动从记录中过滤出id
字段(您不想指定{{1} }插入,因为数据库将自动创建一个。)
关于API参考,除了book之外,还有this API documentation。