Skip to content Skip to sidebar Skip to footer

Many To One Relation Not Working In Fields.selection() In Openerp

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

Solution 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:

<fieldname="project_id"widget="selection"/>

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

deffields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
    if context isNone: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

Post a Comment for "Many To One Relation Not Working In Fields.selection() In Openerp"