Thursday, August 29, 2019

name_search with One2many field in odoo

ODOO

name_search()

how to use name_search() function with One2many field in odoo 11 

to explain the main reason to use this function let us say that we have one2many field in some object and inside this filed there is many2one filed which we have to avoid rebating it 

E.G let us say we have three objects (aobject , bobject, cobject) as below 

class aobject(models.Model):
     _name = "a.object"
     bobject_ids = fields.One2many('b.object', string="B Object", 'aobject_id')

class bobject(models.Model):
     _name = "b.object"
     aobject_id = fields.Many2one('a.object', string="A Object")
     cobject_id = fields.Many2one('c.object', string="C Object")

class cobject(models.Model):
     _name = "c.object"

and we want to avoid re-show the chosen records of cobject_id field when we create records in bobject_ids field in the first object , so we have to ...
 
inside cobject class we have to add name_search() function , just like

class cobject(models.Model):
     _name = "c.object" 
 
     @api.model
     def name_search(self, name, args=None, operator='ilike', limit=100):
     args = args or []
     domain = []
     post = []
     if 'bـobjectـids' in self._context:
     lineـids = self.env['b.object'].resolve_2many_commands('cobject_id' ,self._context.get('bـobjectـids'))
     for line in lineـids:
     if 'aobjectـid' in line:
     if line['aobjectـid']:
     post.append(line['aobjectـid'])
     args.append(('id', 'not in',post))
     return self.search(domain + args).name_get() 

then we go to aobject view file (xml) & add 

<field name="bobjectـids" nolabel="1" >
       <tree editable="bottom">
              <field name="cobjectـid" context="{'bـobjectـids':parent.bobjectـids}"/>
       </tree>
</field>

1 comment:

  1. Can't understand the code,
    please describe something about codes..

    ReplyDelete

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...