pythonodooodoo-8

how to search other field in many2one?


python code:

_name = 'res.partner.table2'

customer_id = fields.Many2one('res.partner', required=True ,string="Customer ID")

XML:

<field name="arch" type="xml">
      <form string="Reward Points">
            <field name="customer_id"/>  //how can i search by res.partner customer_id in here 
      </form>
</field>

I need search by other field instead "name". How is it possible?


Solution

  • We can achieve with following:

    <field name="customer_id" context="{'my_custom_search': True}"/>
    

    We add context to make sure functionality will effect to particular field and not disturb existing functionality.

    class res_partner(models.Model):
        _inherit = 'res.partner'
    
        def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
            if not args:
                args = []
            if not context:
                context = {}
            if context.has_key('my_custom_search'):
                domain = [('field_name', 'operator', value)]
                ids = self.search(domain)
            else:
                return super(res_partner, self).name_search(cr, user, name, args=args, operator='ilike', context=context, limit=limit)
            return self.name_get(cr, uid, ids, context)