我需要在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 :(得分: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