web2py TypeError:id()只接受一个参数(给定0)



我正在Web2Py中制作一个web服务,它接受一些参数并将它们插入到表中。当我在这个1表中插入数据时,我得到了以下错误

 Traceback (most recent call last):
  File "E:DevelopmentPythonweb2pygluonrestricted.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:DevelopmentPythonweb2pygluonglobals.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:DevelopmentPythonweb2pygluondal.py", line 4786, in insert
    return self._db._adapter.insert(self,self._listify(fields))
  File "E:DevelopmentPythonweb2pygluondal.py", line 838, in insert
    query = self._insert(table,fields)
  File "E:DevelopmentPythonweb2pygluondal.py", line 834, in _insert
    values = ','.join(self.expand(v,f.type) for f,v in fields)
  File "E:DevelopmentPythonweb2pygluondal.py", line 834, in <genexpr>
    values = ','.join(self.expand(v,f.type) for f,v in fields)
  File "E:DevelopmentPythonweb2pygluondal.py", line 951, in expand
    return self.represent(expression,field_type)
  File "E:DevelopmentPythonweb2pygluondal.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')  

有什么想法吗?

您在这里有问题:

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

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内置的。我假设你指的是其他变量。

最新更新