Friday, November 27, 2020

many2one field domain from function

 ODOO

 odoo 14


 

in this blog we will explain how we can define domain for Many2one field in odoo 14 , so let us start 

 

First we have "object.a" model which is contain Many2many in which we will add many users , as bellow

user_ids = fields.Many2many('res.users', string="Users")

Second we have "object.b" model , which we have user_id field that contain the current user , and object_a_id which is Many2one field from object "A" , as bellow

user_id = fields.Many2one('res.users', string='Created By', default=lambda self: self.env.user, readonly=True)

object_a_id = fields.Many2one('object.a', string="A")

and we need to make domain over object_a_id field to show only the records from object A that the current user is involved in the user_ids field 

....

To make the domain we have to add function as bellow  

    @api.onchange('some_field_in_B_object')
    def _get_object_b_ids(self):
        """
        function to get the A records for the current user
        """
        object_a_ids = self.env['object.a'].search([])
        ids_lis = []
        domain = [('id','=', -1)]

        for rec in object_a_ids:
            for line in rec.user_ids:
                if self.user_id == line:
                    ids_lis.append(rec.id)
        res = {}
        res['domain'] = {'object_a_id': [('id', 'in', ids_lis)]}
        return res



 

No comments:

Post a Comment

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...