İçereği Atla
Menü
Bu soru işaretlendi
2 Cevaplar
1231 Görünümler

Every project is assigned a partner, the customer.

I have a new field 'customer_contacts' in the project entity.

The 'customer_contacts' should only show the contacts that are related to that customer.

But when I try to add the contacts to the project, they don't show up.


class Partner(models.Model):

_inherit = 'res.partner'

project_id = fields.Many2one(

'project.project',

)


class Project(models.Model):

_inherit = 'project.project'

partner_id = fields.Many2one('res.partner')


customer_contacts = fields.Many2many(

'res.partner',

domain=lambda self: [('id', 'in', self.partner_id.child_ids.ids or [] )]

)

Avatar
Vazgeç
En İyi Yanıt

Hi,

You can use the following code to compute the child contacts of the selected partner.


Python


class ProjectProject(models.Model):

    """

       Inherits the project.project model to associate a customer (partner)

       with its related contacts. Adds functionality to dynamically filter

       customer contacts based on the selected partner.

       """

    _inherit = 'project.project'


    partner_id = fields.Many2one('res.partner', string="Partner")

    partner_contacts_ids = fields.Many2many(

        'res.partner', string="Partner Contacts", column1="partner_id",

        column2="contact_id", compute="_compute_partner_contacts_ids")

    customer_contacts = fields.Many2many(

        'res.partner', string="Customer Contacts",

        domain="[('id', 'in', partner_contacts_ids)]")


    @api.depends('partner_id')

    def _compute_partner_contacts_ids(self):

        """Compute the child contacts of the selected partner (customer) and

        assign them to the partner_contacts_ids field."""

        for rec in self:

            rec.partner_contacts_ids = rec.partner_id.child_ids


XML


<?xml version="1.0" encoding="utf-8"?>

<odoo>

    <record id="edit_project" model="ir.ui.view">

        <field name="name">project.project.view.form</field>

        <field name="model">project.project</field>

        <field name="inherit_id" ref="project.edit_project"/>

        <field name="arch" type="xml">

            <xpath expr="//field[@name='user_id']" position="after">

                <field name="partner_id"/>

                <field name="partner_contacts_ids" invisible="1" widget="many2many_tags"/>

                <field name="customer_contacts" widget="many2many_tags"/>

            </xpath>

        </field>

    </record>

</odoo>


Hope it helps

Avatar
Vazgeç
En İyi Yanıt

Hello @426 SRL, 

You requested a field customer_contacts on the Project model that displays only the child contacts (e.g., individual people) related to the selected customer (partner_id). Below is the code snippet to achieve that

Python

//Code in Comment//

If you have any questions, feel free to contact us.

Hope this helps!


Thanks & Regards,

Email:  odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari

Avatar
Vazgeç

Code:

class Project(models.Model):
_inherit = 'project.project'

customer_contacts = fields.Many2many(
'res.partner',
string='Customer Contacts',
domain="[('parent_id', '=', partner_id)]",
)

İlgili Gönderiler Cevaplar Görünümler Aktivite
1
May 25
748
0
May 25
618
Change filename Çözüldü
1
May 25
636
0
Mar 25
772
0
Oca 25
677