Search code examples
odooodoo-11

odoo filter Many2one on server side


Hello I want to create a privileged contat for each customer company in odoo 11. I succeed in return all contacts in the list of the view by I can't filter it by the selected company. Here is my code:

view:

<odoo>
<data>
    <record id="view_partner_form_inherited" model="ir.ui.view">
        <field name="name">base.partner.form.acftsales.inherit</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="privileged_contact" />
                <field name="companyId" />
            </field>

        </field>
    </record>
</data>
</odoo>

model or getting all contacts (company A, company B,...):

class acftsales_company(models.Model):
    _inherit = "res.partner"    
    companyId=fields.Integer('societe id', compute='_get_value')
    privileged_contact= fields.Many2one('res.partner', string='Contacts', domain=[('active', '=', True),('customer', '=', True)])
    @api.one
    def _get_value(self):
        self.companyId =  self.id 

my failing code in order to filter only for current company:

privileged_contact= fields.Many2one('res.partner', string='Contacts', domain=[('active', '=', True),('customer', '=', True)],('parent_id', '=', self.id)])
privileged_contact= fields.Many2one('res.partner', string='Contacts', domain=[('active', '=', True),('customer', '=', True)],('parent_id', '=', companyId)])
privileged_contact= fields.Many2one('res.partner', string='Contacts', domain=[('active', '=', True),('customer', '=', True)],('parent_id', '=', env['res.partner'].id)])
privileged_contact= fields.Many2one('res.partner', string='Contact commercial', function=_get_employees)
privileged_contact= fields.Many2one('res.partner', string='Contact commercial', function=_get_employees)
@api.multi
def _get_employees(self):
    Partner = self.env['res.partner']
    employees =  Partner.search([('active', '=', True),('customer', '=', True),('parent_id','=',self.id)])
    #privileged_contact
    return employees

Solution

  • I finally succeed in passing all the filter in the view

    privileged_contact= fields.Many2one('res.partner', string='Contacts')
    
    <field name="companyId"  invisible="1" />
    <field name="privileged_contact" domain="[('active', '=', True),('customer', '=', True),('parent_id','=',companyId)]"/>
    

    I hope that this is not pure client side calcul and that it will not slow the application when it will be lot of customers...