0

我需要在 openerp 中创建一个选择字段,它的值应该从一个函数加载,而且这个字段需要与另一个表的 many2one 关系。我已经创建了选择字段并且值是从函数加载的,但是 many2one 关系在其中不起作用。下面给出的是我的代码。

 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"),}
4

1 回答 1

0

将字段 project_id 更改为 many2one,并在字段的视图中添加小部件 ='selection'。在蟒蛇中:

_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
于 2013-05-17T08:39:01.110 回答