Skip to Content
Menu
This question has been flagged
3 Replies
26196 Views

Hello guys!

I really have problem with domain for one2many field.

In the res.company form, I select some partners for the field line_of_business_ids. I save this and it works.

In the view view_partner_property_form (see below) the field line_business_ids of the partner should only offer the values available in the field line_of_business_ids of his own and only company_id. Every partner has a company_id field. And any company has a line_of_business_ids field (in my case). 

I'm not able to write this domain myself.

Could you help me?

Thanks to help

I actually have this code :

In Python

class Partner(models.Model):
_inherit = "res.partner"
     related_business_id = fields.Many2one('res.partner', string='Related business line')
     line_business_ids = fields.One2many(comodel_name='res.partner', inverse_name='related_business_id')

class res_company(models.Model):
_inherit = "res.company"
     line_of_business_ids = fields.Many2many(comodel_name='res.partner')

In the xml view

<record id="view_partner_property_form" model="ir.ui.view">
    <field name="name">base.view.partner.form.inherit.vtm2</field>
     <field name="model">res.partner</field>
     <field name="inherit_id" ref="base.view_partner_form" />
     <field name="arch" type="xml">
         <field name="category_id" position="after">
            <field name="line_business_ids" widget="many2many_tags"
                attrs="{'invisible': [('is_company','=', False)]}"
                options="{'no_create_edit': True}"
                domain="[('id', 'in', 'company_id.line_of_business_ids.ids')]" />
        </field>
    </field>
</record>


See image of the situation



Edit #1

I have tried all this domains. No success...

domain="[('company_id.line_of_business_ids', 'in', id )]"

domain="[('id', 'in', company_id.line_of_business_ids)]

domain="[ ( 'company_id.line_of_business_ids', '=', [ (6, [0], [id]) ] ) ]

domain="[('company_id.line_of_business_ids.id', 'in', company_id.line_of_business_ids )]"

domain="[('company_id.line_of_business_ids', '=', company_id.line_of_business_ids.ids )]"

domain="[('company_id.line_of_business_ids', '=', company_id.line_of_business_ids.id )]"

domain="[('company_id.line_of_business_ids', '=', company_id.line_of_business_ids.partner_id.id )]"

domain="[('company_id.line_of_business_ids', 'in', company_id.line_of_business_ids )]"

domain="[('company_id.line_of_business_ids', 'in', company_id.line_of_business_ids[0][2] )]"


Avatar
Discard
Best Answer

Hi, 

Pardon me if I misunderstood your requirement. But please clarify the following.

Why can't you use a related One2many field on res.partner to company_id.line_of_business_ids, which could be a readonly field. Since a partner is supposed to have only one company_id, I think no need to use a Many2many field in res.company, you could have use a One2many field. For related fields, both field types should be same.

Then on the partner form, in line_business_ids you can only see the partners added on his company.

For eg:

class Partner(models.Model):
    _inherit = "res.partner"
    line_business_ids = fields.One2many(related='company_id.line_of_business_ids', readonly=True, string="Line business ids")

class res_company(models.Model):
    _inherit = "res.company"
    line_of_business_ids = fields.One2many('res.partner','company_id',string="Line of business ids")
Avatar
Discard
Author

Mister Sivan, I want to send you 1000 thanks for this precious answer. I will publish my solution soon.

A chance you have been there!

Good to know that my answer helps you :)

Author Best Answer

Big thanks to mister Sivan for his help. It was precious!

Here is our complete solution

In Python, with more significant names

class Partner(models.Model):
    _inherit = "res.partner"

    customer_of_division_id = fields.Many2one(comodel_name='res.partner', domain="[('division_of_company_id','=',company_id)]", string='Customer of the division')
    division_of_company_id = fields.Many2one('res.company', string='Division of the company')

class res_company(models.Model):
    _inherit = "res.company"
   
    division_ids = fields.One2many('res.partner','division_of_company_id', string="Divisions of this company")


In views

<record id="view_company_form" model="ir.ui.view">
            <field name="name">base.view.company.form.inherited.vtm2</field>
            <field name="model">res.company</field>
            <field name="inherit_id" ref="base.view_company_form" />
            <field name="arch" type="xml">
                <field name="website" position="after">
                    <field name="division_ids" widget="many2many_tags" string="Divisions of this company" />
                </field>
            </field>
</record>


<record id="view_partner_property_form_inherit" model="ir.ui.view">
            <field name="name">base.view.partner.form.inherit.vtm2</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form" />
            <field name="arch" type="xml">
                <field name="category_id" position="after">
                      <field name="customer_of_division_id" />
                    <field name="division_of_company_id" />
               </field>
            </field>
</record>


In images






Avatar
Discard
Related Posts Replies Views Activity
1
Oct 22
2971
1
Aug 19
2090
1
Sep 17
5094
1
Aug 16
8516
5
Feb 17
5717