Many to one relation not working in fields.selection() in openerp

雨燕双飞 提交于 2019-12-25 02:04:40

问题


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:


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


来源:https://stackoverflow.com/questions/16578570/many-to-one-relation-not-working-in-fields-selection-in-openerp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!