我在Web2Py中创建一个Web服务,它接受一些参数并将它们插入到表中。我在这1个表中插入数据时出现以下错误
Traceback (most recent call last):
File "E:\Development\Python\web2py\gluon\restricted.py", line 192, in restricted
exec ccode in environment
File "E:/Development/Python/web2py/applications/my_app/controllers/mobi.py", line 96, in <module>
File "E:\Development\Python\web2py\gluon\globals.py", line 145, in <lambda>
self._caller = lambda f: f()
File "E:/Development/Python/web2py/applications/my_app/controllers/mobi.py", line 81, in createwish
wish_id = db.t_wish.insert(f_user_id = id, f_category_id = category_id, f_sub_category_id = sub_category_id, f_min_price = min_price, f_max_price = max_price, f_location_range = location_range, f_keywords = keywords)
File "E:\Development\Python\web2py\gluon\dal.py", line 4786, in insert
return self._db._adapter.insert(self,self._listify(fields))
File "E:\Development\Python\web2py\gluon\dal.py", line 838, in insert
query = self._insert(table,fields)
File "E:\Development\Python\web2py\gluon\dal.py", line 834, in _insert
values = ','.join(self.expand(v,f.type) for f,v in fields)
File "E:\Development\Python\web2py\gluon\dal.py", line 834, in <genexpr>
values = ','.join(self.expand(v,f.type) for f,v in fields)
File "E:\Development\Python\web2py\gluon\dal.py", line 951, in expand
return self.represent(expression,field_type)
File "E:\Development\Python\web2py\gluon\dal.py", line 1266, in represent
obj = obj()
TypeError: id() takes exactly one argument (0 given)
模型就是这个
########################################
db.define_table('t_wish',
Field('id','id',
represent=lambda id:SPAN(id,' ',A('view',_href=URL('wish_read',args=id)))),
Field('f_user_id', type='string',
label=T('User Id')),
Field('f_category_id', type='string',
label=T('Category Id')),
Field('f_sub_category_id', type='string',
label=T('Sub Category Id')),
Field('f_min_price', type='integer',
label=T('Min Price')),
Field('f_max_price', type='integer',
label=T('Max Price')),
Field('f_location_range', type='integer',
label=T('Location Range')),
Field('f_keywords', type='string',
label=T('Keywords')),
Field('is_active','boolean',default=True,
label=T('Active'),writable=False,readable=False),
Field('created_on','datetime',default=request.now,
label=T('Created On'),writable=False,readable=False),
Field('modified_on','datetime',default=request.now,
label=T('Modified On'),writable=False,readable=False,
update=request.now),
Field('created_by',db.auth_user,default=auth.user_id,
label=T('Created By'),writable=False,readable=False),
Field('modified_by',db.auth_user,default=auth.user_id,
label=T('Modified By'),writable=False,readable=False,
update=auth.user_id),
format='%(f_user_id)s',
migrate=settings.migrate)
db.define_table('t_wish_archive',db.t_wish,Field('current_record','reference t_wish'))
插入数据的代码是
#Creating a wish
def createwish():
try:
user_id = request.vars['id']
category_id = request.vars['category_id']
sub_category_id = request.vars['sub_category_id']
min_price = request.vars['min_price']
max_price = request.vars['max_price']
location_range = request.vars['loc_range']
keywords = request.vars['keywords']
except:
raise HTTP(400, 'BAD REQUEST: Requires all params - id, category_id, sub_category_id, min_price, max_price, loc_range, keywords')
# try:
wish_id = db.t_wish.insert(f_user_id = id, f_category_id = category_id, f_sub_category_id = sub_category_id, f_min_price = min_price, f_max_price = max_price, f_location_range = location_range, f_keywords = keywords)
return sj.dumps({'result':'success', 'wish_id':wish_id})
#except:
# raise HTTP(500, 'Internal Server Error')
有什么想法吗?
答案 0 :(得分:3)
你有问题:
db.define_table('t_wish',
Field('id','id',
represent=lambda id:SPAN(id,' ',A('view',_href=URL('wish_read',args=id)))),
在这里:
# try:
wish_id = db.t_wish.insert(f_user_id = id, ...)
更改为:
db.define_table('t_wish',
Field('id','id',
represent=lambda value:SPAN(value,' ',A('view',_href=URL('wish_read',args=id)))),
和
# try:
wish_id = db.t_wish.insert(f_user_id = user_id, ....)
你不能使用'id',因为它是一个内置的Python
答案 1 :(得分:2)
File "E:/Development/Python/web2py/applications/my_app/controllers/mobi.py", line 81, in createwish
wish_id = db.t_wish.insert(f_user_id = id, f_category_id = category_id, f_sub_category_id = sub_category_id, f_min_price = min_price, f_max_price = max_price, f_location_range = location_range, f_keywords = keywords)
请注意f_user_id = id
,即python builtin。我假设你的意思是其他变量。