Many to one relation not working in fields.selecti

2019-08-30 11:10发布

I need to create a selection field in openerp , it's values should load from a function and also this field needs many2one relation with another table.I have created the selection field and values are loaded from the function but many2one relation not working in it.below given is my code.

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

1条回答
做个烂人
2楼-- · 2019-08-30 11:20

change the field project_id to many2one and in the view for the field add widget='selection'. in python:

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

in xml:

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

then override the fields_view_get function and add the filter condition for project_id. For example

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
查看更多
登录 后发表回答