Odoo:两个选择字段,第二个选择取决于第一个选定的值

时间:2016-06-14 15:51:11

标签: openerp selection onchange

我有一个带有两个选择字段的模型。在第一个选择字段中,用户选择一个项目,然后用户选择一个部分,取决于所选的第一个字段。 (对于一个项目,我们有很多部分) 我将这个声明用于我的功能:

 @api.multi
 @api.onchange('project')
 def _get_tranche(self):
     print res
     return res`

如果我将在控制台中打印返回值的结果是正确的,并且我具有我搜索的内容,但在视图中,第二个字段值不会显示在设置第二个字段值后要选择的计算值。 有什么方法可以使它正常工作吗?

这是我的代码:

@api.model
def _get_project(self):

    values = {'$top' : 100,
            '$filter' : "'name==:1&&parent==null'",
            '$params' : '\'["*"]\'' }
    response = requests.get('http://localhost:8081/rest/Project/name',
                            auth=('admin', 'admin'),
                            params=values)
    data = response.json()
    res = []
    for record in data['__ENTITIES']:
        res.append((record['__KEY'], record['name']))
    return res

@property
def ret_value(self):
    return self.project

@api.multi
@api.onchange('project')
def _get_tranche(self):
        if self.ret_value == False:
            return []

        values = {'$top' : 100,
                '$filter' : "'parent.ID==:1'",
                '$params' : '\'[' + self.ret_value + ']\'' }
        response = requests.get('http://localhost:8081/rest/Project/name',
                                auth=('admin', 'admin'),
                                params=values)
        data = response.json()
        res = []
        for record in data['__ENTITIES']:
            res.append((record['__KEY'], record['name']))
        print res
        return res

_columns = {
    'name': fields.char("Name", required=True),

    'description' : fields.text(),

    'project' :fields.selection(selection=_get_project, string="Project"),
    'tranche' : fields.selection(selection=_get_tranche, string="Tranche"),
}

注意:一个tranche是一个项目,其中parent_id(非null)=项目的id,我需要执行以下操作:当我从第一个字段选择“项目”中选择项目X时,我可以选择此项目的部分项目(项目与parent_id = X)从第二个字段选择“tranche”我无法理解的是在控制台中打印的结果是正确的,但在视图中字段tranche是空的?? !!

现在我正在尝试这个:

        _columns = {
    'name': fields.char("Name", required=True),

    'description' : fields.text(),

    'project' :fields.selection(selection=_get_project, string="Project"),
    'tranche' : fields.selection(selection=_get_tranche,
 string="Tranche", compute='_return_tranche'),
 }
@api.depends('project')
def _return_tranche(self):
        if self.ret_value == False:
            self.tranche = []
            return
        values = {'$top' : 100,
                    '$filter' : "'parent.ID==:1'",
                    '$params' : '\'[' + self.ret_value + ']\'' }
        response = requests.get('http://localhost:8081/rest/Project/name',
                                auth=('admin', 'admin'),
                                params=values)
        data = response.json()
        res = []
        for record in data['__ENTITIES']:
            res.append((str(record['__KEY']),str(record['name']))) 
        print res
        self.tranche = res
        return

@api.model
def _get_tranche(self):

        values = {'$top' : 100,
                    '$filter' : "'name==:1&&parent!=null'",
                    '$params' : '\'["*"]\'' }
        response = requests.get('http://localhost:8081/rest/Project/name',
                                 auth=('admin', 'admin'),
                                 params=values)
        data = response.json()
        res = []
        for record in data['__ENTITIES']:
            res.append((record['__KEY'], record['name']))
        return res

但是我收到了这个错误

File "/opt/odoo/odoo/openerp/fields.py", line 1529, in convert_to_cache
raise ValueError("Wrong value for %s: %r" % (self, value))
ValueError: Wrong value for crm.lead.tranche: [('6', 'Tranche I'), ('7', 'Tranche II'), ('8', 'Tranche III')]

请帮帮我......

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

 @api.multi
 @api.onchange('project')
 def _get_tranche(self):
    result = []
    for record in self:
        result.append((record.id, res))
    return result
相关问题