Skip to Content
मेन्यू
This question has been flagged
1 Reply
155 Views

I have a model defined like this:


```

analyst_id = fields.Many2one(

"res.partner",

string="Analyst",

required=True,

domain=[

("is_company", "=", False),

# ("parent_id", "=", self.env.company.partner_id),

],

help="Analyst who performed this analysis",

default=lambda self: self.env.user.partner_id,

)

```


When I comment out the parent_id domain, the dropdown breaks: it opens and closes right away as if there are no results.


The fields should be correct since I check these in validation too and there it works.


In essence I want the dropdown to only show contacts that belong to the contact of the current company. Analysts are not users in Odoo so we cannot reference users here.

Avatar
Discard
Best Answer

Hi,


To solve the issue, you have two good options:

1- Use a domain with a callable (Python)

If the domain depends on runtime values like the current company, you should implement it with a method:

analyst_id = fields.Many2one(

    "res.partner",

    string="Analyst",

    required=True,

    domain=lambda self: [

        ("is_company", "=", False),

        ("parent_id", "=", self.env.company.partner_id.id),

    ],

    help="Analyst who performed this analysis",

    default=lambda self: self.env.user.partner_id,

)


Here the lambda self: ensures the domain is evaluated at runtime, so it will correctly use the current company’s partner.


2- Use domain in the XML view instead


You can leave the Python field definition simple and add the runtime-specific filter in the view:<field name="analyst_id"

       domain="[('is_company','=',False), ('parent_id','=', company_id)]"/>



This way, Odoo evaluates the domain dynamically in the client based on the current record.


Since you want to restrict analysts to the contacts under the current company’s partner, Option 1 with the Python lambda is usually cleaner if this should always apply system-wide. Option 2 makes sense if you only want this restriction in specific views.



Hope it helps




Avatar
Discard
Author

Thanks, I actually had the lambda implementation before, but the crucial part was the .id after partner_id here.

So this works perfectly indeed:

analyst_id = fields.Many2one(
"res.partner",
string="Analyst",
required=True,
domain=lambda self: [
("is_company", "=", False),
("parent_id", "=", self.env.company.partner_id.id),
],
help="Analyst who performed this analysis",
default=lambda self: self.env.user.partner_id,
)

Thanks!

Related Posts Replies Views Activity
2
सित॰ 25
352
3
सित॰ 25
358
1
सित॰ 25
390
2
अग॰ 25
591
2
अग॰ 25
694