Skip to Content
Menu
This question has been flagged
2 Replies
14545 Views

Hi, 

I'm new to Odoo /OpenERP and I'm trying to extend the sale module by adding a new field "linked" 

to another field. To be more clear,  I want to populate the new field "company_contact_id" when the field "company_id" is modified.


Here's my code

class sale_order(models.Model):
_inherit = 'sale.order'

company_contact_id = fields.Many2one('res.partner', string='Company contact', required=False, ondelete='restrict')

@api.onchange('company_id')
def _onchange_company_id(self):
        partner_ids = self.env['res.partner'].search([('parent_id', '=', self.company_id)])
        return {'domain': {'company_contact_id': ['id', 'in', partner_ids]}}

After updating the field "company_contact_id" is created in the table "sale_order" but when I try to change the field "company_id"

I get the following error :


AssertionError: Invalid value res.company(3,) in domain term ('parent_id', '=', res.company(3,))


I have no idea how to fix this.

Any idea ? Thanks


Avatar
Discard
Best Answer

Hi 

The issue is with the value inside the domain. You are passing company recordset (self.company_id) instead of company ID (self.company_id.id).

Try this:

    partner_ids = self.env['res.partner'].search([('parent_id', '=', self.company_id.id)])

Avatar
Discard
Author Best Answer

@Suhdir Arya,

Thank you for your answer. I tried this too but the error was more confusing to me.

TypeError: res.partner() is not JSON serializable


Thanks


EDIT:

OK I think it's ok now. I had to return the domain this way :

'domain': {'document_contact_id': ['id', 'in', partner_ids.id]


Avatar
Discard

Yes, that was another error which I missed in your code. :)

You should not pass recordset, pass ID(s) instead.

Use partner_ids.ids instead of id because when you will have multiple matches from search method, then you will face sigletone error.