3
votes

Here's my class:

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

then in fields_view_get method i want to iterate through employee_ids and make list of each employee.

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

how can i do it? thanks

1
What you mean with "Make list of each employee?" - dccdany
I mean iterate through each employee in many2many field, and for example append each employees id to list - Nikoloz Akhvlediani
Thing is, you need something to get the active_ids, im not sure where ure running this function from, it doesnt have "id" or "ids" as a parameter so you cant take the actual record to iterate, plus, it is from old api so you cant use self as the actual record. - dccdany

1 Answers

1
votes

I am supposing help_desk model have 3 fields 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")

Now if you want to apply some logic so go through the below mention lines.

Well method like fields_get ,fields_view_get help us in improving UI experience by applying the filter/domain on fly. so the code is here:

@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 

here i have put the domain on Many2one and Many2many and add some dynamic list based on domain inside the Selection field.

you can also refer account/models/chart_template.py.