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

So I'm trying to show the contacts name in a company with this model code:

location_id = fields.Many2one('res.partner', string='Show Location')
location_contact_id = fields.Char(related='location_id.name', string='Location Contact Name', readonly=True)



Lets say the company is called "Company1" and the contact is "Joe Doe". This shows "Company1" but not "Joe Doe". Do I need to change the code to:


related='location_id.childs_id.name'



Adding on to this, what if there are multiple contacts and I only want the first contact to show up?

Avatar
Discard
Best Answer

To display the name of the first contact associated with a company in the `location_contact_id` field, you can make use of a related field and modify the code as follows:

location_contact_id = fields.Char(related='location_id.child_ids[0].name', string='Location Contact Name', readonly=True)


In this code, `location_id.child_ids` is a One2many field on the `res.partner` model that represents the contacts associated with a company. By using `[0]`, we are accessing the first record in the list of child contacts. We then retrieve the `name` field of that contact to display in the `location_contact_id` field.

Please note that if there are no contacts associated with the company or if the company has multiple contacts, this code will result in an error. To handle such scenarios, you can add additional checks or validations to ensure that the related field only retrieves the first contact when it exists.

Alternatively, you can also consider using computed fields to implement more complex logic to determine which contact should be displayed based on your specific requirements.

Avatar
Discard
Best Answer

Hi David White,So far what I understand from your question is that, "you have a contactrelated with the company details and that company also has some child contactsrelated with this. So you want to show the first listed contact in thelocation_contact_id field." You can achieved this using compute field. below I have share the sample code.

from odoo import models,fields, api

class ResComapny(models.Model):
​_inherit = 'res.company'
location_id = fields.Many2one('res.partner', string='Show Location')
location_contact_id = fields.Many2one('res.partner',string="ocation Contact Name", compute='_compute_location_contact_id')

@api.depends('location_id')
​def _compute_location_contact_id(self):
for record in self:
​if record.location_id.child_ids:
​record.location_contact_id = record.location_id.child_ids[0].id

you can also store it. by adding parameter store=True in the location_contact_id fields and by the way if you want to showthe comapny dependent contacts only then res.company model already has partner_id. in this case you don't have to add this new field location_id. just replace location_id with partner_id field. Thank you.


Thanks & Regards, 

 

Brain Station 23 Ltd. 

Mobile: (+880) 1404055226 

Email: sales@brainstation-23.com 

Web: https://brainstation-23.com/ 

Address: (Building-1) 8th Floor, 2 Bir Uttam AK Khandakar Road, Mohakhali C/A, Dhaka 1212, Bangladesh 

Avatar
Discard
Best Answer

Hello David White,

- If you consider selecting a Partner ‘YourCompany’ then because of the related field it will provide you the name with “YourCompany”.
- So if you want to show the first contact of this company then you can follow below logic.
- Where this below logic can also be performed under create and write method.

Please find code in comment. 

I hope this can help you.

Thanks & Regards,
Email:   odoo@aktivsoftware.com 

Skype: kalpeshmaheshwari


Avatar
Discard

Please find code here :-

@api.onchange('location_id')
def onchange_location_id(self):
contact = list(map(lambda child: child , self.location_id.child_ids )
if contact:
self.location_contact_id = contact[0].name