Elhagag Abdalla
Elhagag Abdalla

Reputation: 33

Domain field many2one based on another many2one field Odoo 11

hello everyone I am odoo newbies I have created a module to Modify the Accounting module. I want to modify vendor bills and customer invoices and ai have added a new column called Budget to fetch all the budget it's a many2one field I want when I select a budget to show me only the analytic account related to it.

class custom_accounting_edit(models.Model):
    _inherit = 'crossovered.budget.lines'

    analytic_account_id = fields.Many2one('account.analytic.account', 'Budget line')


class BillsEdit(models.Model):
    _inherit = 'account.invoice.line'

    budget_id = fields.Many2one('crossovered.budget', string='Budget')
    account_analytic_id = fields.Many2one('account.analytic.account',
                                          string='Budget line')

    @api.onchange('budget_id')
    def onchange_analytic_account_id(self):
        for rec in self:
            return {'domain': {'analytic_account_id': [('analytic_account_id', '=', rec.budget_id.id)]}}

enter image description here

Upvotes: 1

Views: 1291

Answers (2)

Kenly
Kenly

Reputation: 26688

The method is invoked on a pseudo-record that contains the values present in the form, so you do not need to loop over self.

You need to use id field in the domain to filter accounts using the value of budget lines analytic account (It will return the id field value).

You can use mapped which will return the union of all budgets analytic accounts, with duplicates removed.

@api.onchange('budget_id')
def onchange_analytic_account_id(self):
    return {'domain': {'account_analytic_id': [
        ('id',
         'in',
         self.mapped('budget_id.crossovered_budget_line.analytic_account_id.id')
         )]
    }
    }

Upvotes: 1

SDBot
SDBot

Reputation: 834

Try this

@api.onchange('budget_id')
def onchange_analytic_account_id(self):
    account_ids = [line.analytic_account_id.id for line in self.budget_id.crossovered_budget_line]
    return {'domain': {'analytic_account_id': [('id', 'in', account_ids)]}}

Upvotes: 0

Related Questions