AnJ
AnJ

Reputation: 614

Compute many2many field with values from another model using Odoo developer interface

I need a new field inside Contact model that would hold information about Allowed companies of the related user.

Now there is only field about Currently picked company by that user (and it is not enough for me to make a record rule). The field I want to copy values from is inside model Users and it is called company_ids.

I’m trying to add this field in the developer mode (Settings > Technical > Fields) like this: creating new field

But I’m having trouble with code that would fill my field with values from the another model.

for record in self:
  record[("x_company_ids")] = env['res.users'].company_ids

I’m guessing that the record is referring to a record inside Contact model and it does not contain fields from another models like Users. So I can’t figure it out how to reference a field from another model. Something similar to this: env['res.users'].company_ids?

It is even harder for me because it is many2many field and should always update when the source changes.

Maybe better solution would be to use Automatic action to write values to this field?

I saw some threads like this: Computed many2many field dependencies in Odoo 10. But it seems like in those cases they had clear connection between the fields and I don't have it. I don't know how to get related user while I'm inside Contact model. I know only how to this oposite way (from user to contact): user.partner_id.id

Upvotes: 0

Views: 1716

Answers (1)

Mital Vaghani
Mital Vaghani

Reputation: 321

Here in below given code you haven't specified related user from which you will get company_ids, you have directly accessing company_ids

for record in self:
  record[("x_company_ids")] = env['res.users'].company_ids

You can write as following :

for record in self:
  record["x_company_ids"] = self.env['res.users'].search([('partner_id','=',record.id)]).company_ids

Upvotes: 2

Related Questions