多对一关系在openerp的field.selection()中不起作用 [英] Many to one relation not working in fields.selection() in openerp

查看:93
本文介绍了多对一关系在openerp的field.selection()中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

将字段project_id更改为many2one,并在该字段的视图中添加widget ='selection'. 在python中:

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

在xml中:

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

然后覆盖fields_view_get函数,并为project_id添加过滤条件.例如

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

这篇关于多对一关系在openerp的field.selection()中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆