Openerp:迭代Many2Many字段

时间:2016-04-13 08:29:22

标签: python openerp odoo-8

这是我的班级:

employee_ids = fields.Many2many('hr.employee', string="Empls")
status = fields.Selection([
        ('draft', 'Draft'),
        ('done', 'Done'),
        ])

然后在fields_view_get方法中,我想遍历employee_ids并列出每个员工的列表。

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
        if context is None:
            context = {}
        res = super(help_desk, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
        #here i want to iterate throught employee_ids and make list of each employee
        for f in res['fields']:
            if f == 'status':
                res['fields'][f]['selection'] = #put list here
        return res

我该怎么办?感谢

1 个答案:

答案 0 :(得分:1)

我假设help_desk模型有3个字段Selection,Many2one and Many2many

        status = fields.Selection([
                ('draft', 'Draft'),
                ('done', 'Done'),
                ])
        partner_id = fields.Many2one(comodel_name='res.partner', string='Partner')
        employee_ids = fields.Many2many('hr.employee', string="Empls")

现在,如果你想应用一些逻辑,那么请看下面提到的行。

像[{1}}这样的方法可以帮助我们通过动态应用过滤器/域来改善UI体验。 所以代码在这里:

fields_get ,fields_view_get

此处我已将域名放在@api.model def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): res = super(help_desk, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) PartnerObj= self.env['res.partner'] domain = [('phone','!=',False)]# put your domain or just place blank list partners = PartnerObj.search(domain) if partners for field in res['fields']: # if field == 'partner_id': # res['fields'][field]['domain'] = [('id', 'in', partners.ids)] elif field == 'employee_ids': res['fields'][field]['domain'] = [('id', 'in', partners.ids)] elif field=='status': # Appending the partners in status doesn't making any sense but as per your words "put list here" res['fields'][field]['selection'] = partners and [(partner.id, partner.name) for partner in partners] or [('', '')] return res 上,并根据Many2one and Many2many字段内的域添加一些动态列表。

您也可以参考account/models/chart_template.py.