2011-08-20 113 views
4

我正在Web2Py中接受一些參數並將它們插入到表中的Web服務。我收到以下錯誤,而我在這1個表web2py TypeError:id()只需要一個參數(0給出)

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') 

任何想法?

回答

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, ....) 

,因爲它是建立在

+1

不能使用「身份證」'id'不是關鍵字:'>>> import關鍵字\ n > >> keyword.iskeyword('id')\ n False這是一個內建的。儘管如此,你不應該給變量名稱與內建函數衝突,因爲它會導致這種錯誤。 – hughdbrown

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。我假設你的意思是其他一些變量。

+1

Python的是,他的意思'user_id'作爲設定幾條路線以上 –

相關問題