This question has been flagged
2 Replies
5303 Views

Dear All,

We are in development of store request module, that a department user request items to store and they will deliver based on the request. So in department module we created dept_location_id = fields.Many2one('stock.location', 'Department Location'). Its working fine. My problem is I need this value in my custom module store request. So i create a compute function in store request module. Raise error is showing correct location id, But the function is not returning any value. Any help is highly appreciated. Thanks.

 #---------------------------To find Department warehouse Location----------------------

    @api.one
    @api.depends('partner_id')
    def _compute_department_location(self):
   
        loc_id = 1
        if (self.partner_id.id == False):
            #self.department_id = None
            loc_id = None
            return
       
        employee = self.env['hr.employee'].search([('work_email', '=', self.partner_id.email)])
        if (len(employee) > 0):
            self.department_id = employee[0].department_id.id
            dept_id = self.env['hr.department'].search([('id', '=', self.department_id.id)])
            if (len(dept_id) > 0):
                loc_id = dept_id[0].dept_location_id.id
#              raise UserError(_('Test - %s') % loc_id)
            else:
                loc_id = None
        else:
 
            loc_id = None
#        raise UserError(_('Test - %s') % loc_id)
        return loc_id

    dept_location_id = fields.Many2one('stock.location', 'Department Location',
        compute='_compute_department_location',ondelete="cascade",store=True)



Avatar
Discard
Best Answer

As a single compute function can be used to compute multiple fields, it doesn't return a value. Instead, the value should be assigned in the function. 

If you change return loc_id to self.dept_location_id = loc_id, it should write the value.
Note: Compute functions in v11 should be able to handle recordsets. You should wrap the body of the function with for rec in self and do the same logic using rec instead of self.

Avatar
Discard
Author

Thanks Jake, Its working....