Search code examples
odooodoo-8odoo-9

Retrieving multiply ids of same table


So every partner have this fields that I added to list fields in my method. those fields are many2one fields with comodel 'product.category' so they are in the same table. how can I retrieve id's of all this field for a current partner?

class ClientPotential(models.Model):
    _name = 'client.potential'
    _description = 'Client Potential'
    partner_id = fields.Many2one(
            'res.partner', string='Client')

@api.onchange('partner_id', 'categ_id')
    def _calculate_potential(self):
        categ_obj = self.env['product.category']
        fields = ['flexible_id', 'flexible_qty', 'drawer_id', 'drawer_qty',
                  'runner_id', 'runner_qty', 'group_1_id', 'group_1_qty', 'group_2_id',
                  'group_2_qty', 'group_3_id', 'group_3_qty']

        for field in fields:
            categ_obj += 
        ?????

Solution

  • I think getattr will do the trick:

        categ_obj = self.env['product.category']
        fields = ['flexible_id', 'flexible_qty', 'drawer_id', 'drawer_qty',
                  'runner_id', 'runner_qty', 'group_1_id', 'group_1_qty', 'group_2_id',
                  'group_2_qty', 'group_3_id', 'group_3_qty']
        if self.partner_id:
            for field_name in fields:
                categ_obj += getattr(self.partner_id, field_name)
    

    Hope you got the idea

    Man I love python because of this.