2013-05-16 50 views
0

我需要在openerp中創建一個選擇字段,它的值應該從一個函數加載,而且這個字段需要與另一個表的多個二元關係。我已經創建了選擇字段和值從函數加載,但許多二元關係不起作用。下面給出的是我的代碼。多對一的關係在fields.selection()中不起作用openerp

def _sel_proj(self, cr, uid, context=None): 
    cr.execute("""SELECT project.id,account.name FROM project_project project 
         LEFT JOIN account_analytic_account account ON 
            account.id = project.analytic_account_id 
         LEFT JOIN project_user_rel rel ON rel.project_id = project.id 
         WHERE (account.user_id = %s or rel.uid = %s) 
         GROUP BY project.id,account.name"""%(uid, uid)) 
    return [(r[0],r[1]) for r in cr.fetchall()] 

    _name = 'mat.mgmt' 
    _columns = {'project_id':fields.selection(_sel_proj,string='Project',type="many2one",relation="project.project",select="true",required="true"),} 
+0

使用many2one字段並覆蓋def search()函數以實現此目的 – senthilnathang

回答

0

將字段project_id更改爲many2one,並在字段視圖中添加widget ='selection'。 在python:

_columns = {'project_id':fields.many2one('project.project','Project',select="true",required="true"),} 

在XML:

<field name="project_id" widget="selection"/> 

然後重寫fields_view_get函數並添加過濾條件PROJECT_ID。例如

def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False): 
    if context is None:context = {} 
    res = super(<your_class_name>,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu) 
    for field in res['fields']: 
     if field == 'project_id': 
      cr.execute("""SELECT project.id,account.name FROM project_project project 
        LEFT JOIN account_analytic_account account ON 
           account.id = project.analytic_account_id 
        LEFT JOIN project_user_rel rel ON rel.project_id = project.id 
        WHERE (account.user_id = %s or rel.uid = %s) 
        GROUP BY project.id,account.name"""%(uid, uid)) 
      project_select = [(r[0],r[1]) for r in cr.fetchall()] 
      res['fields'][field]['selection'] = project_select 
    return res 
相關問題