Mital Vaghani
Mital Vaghani

Reputation: 321

How to get values in onchange with sudo()

I have added one onchange method, in that onchange method I have used sudo() while accessing many2one field.

But with sudo(), I am not able to get record's values with sudo. So how can I get values of onchange record (<odoo.models.NewId object at 0x7fba62f7b3d8>) with sudo().

Here is sample code :

@api.onchange('product_id')
    def onchange_product_id(self):
        for record in self:
            print(record.product_id)
            print(record.sudo().product_id)

Actual result :

product.product(13,)
product.product()

Expected result :

product.product(13,)
product.product(13,)

Upvotes: 1

Views: 501

Answers (1)

Adan Cortes
Adan Cortes

Reputation: 1068

That's because the recordset doesn't exist outside the current transaction. So your current user can see the contents but other users can't.

The code looks good to me, in fact, if you see path_to_v12/addons/hr_expense/models/hr_expense.py lines 563-567, you'll see a similar code:

@api.onchange('employee_id')
def _onchange_employee_id(self):
    self.address_id = self.employee_id.sudo().address_home_id
    self.department_id = self.employee_id.department_id
    self.user_id = self.employee_id.expense_manager_id or 
    self.employee_id.parent_id.user_id

Upvotes: 0

Related Questions