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">
<tree editable="bottom">
<field name="cobjectـid" context="{'bـobjectـids':parent.bobjectـids}"/>
</tree>
</field>
Can't understand the code,
ReplyDeleteplease describe something about codes..