Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
14474 มุมมอง

Let´s say I authenticate  in Odoo as user A. Then I call a method in a model. Then I want to call another method in another model, but as a different user.

Something like:

def my_model_method(self, cr, uid):
other_user_id = authenticate(other_user_login, other_user_password) # authenticate as another user
modelXHandler.othermethod(cr, other_user_id, parameters) # Call another method as the other user


อวตาร
ละทิ้ง

Thanks a lot Axel.

I will try that.

คำตอบที่ดีที่สุด

You can do it like this:

def my_model_method(self, cr, uid):
modelXHandler.othermethod(cr, other_user_id, parameters)

or using the new API sudo method:

@api.model #or @api.multi depending if the method is called on a recordset or not
def my_model_method(self):
self.sudo(user=other_user_id).othermethod(parameters)

for both cases you need the id of the other user that could be obtained from a search on the res.users model or any other way

But if you get it by calling the authenticate on controller or specifically on the session api you will end up with the current session bind completely to the other user id and maybe that's not what you need. Just using the other_user_id like in the examples above you could do it

อวตาร
ละทิ้ง

In Odoo 13 we use with_user(uid) for this purpose, like

modified_attendance = employee.with_user(employee.user_id)._attendance_action_change()

คำตอบที่ดีที่สุด

If you need to run the function with admin rights - use self.sudo()

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

self.sudo().method(...) => run the method as the administrator (no parameter needed in this case). This is most likely what you try to achieve.


self.sudo(10).method(...) => run the method as the user with id 10. Of course do not use a static ID, do a search on res.users to find the ID you need

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
Expected singleton แก้ไขแล้ว
9
มี.ค. 20
42205
0
ธ.ค. 19
4201
2
ต.ค. 18
4097
1
ต.ค. 17
4347
1
มี.ค. 17
10779