Skip to Content
Menu
This question has been flagged
2 Replies
9234 Views

want current user department and this code works fine but only on admin user

dep = fields.Many2one('hr.department', string='Administrative unit', default=lambda self: self.env.uid, readonly=True)

when i log in with another user, can't create and get error

Record does not exist or has been deleted.

(Records: [6], User: 6)


thanks in advance

Avatar
Discard
Best Answer

Hi,

The Given code is wrong, you are mapping the user id to the hr.department model. What happens is that, you have to store the id of the department to the field dep, instead of giving the id of department, you are passing the id of the user as default value.

For example,

In departments table/model, you have 3  records, ABC with iD 1, XYZ with ID 2 and LMN with ID 3. Now user with the ID 2 logged into system, the assigned department will be XYZ as its ID is 2 and now if user with ID 1 is logged in the assigned department will be ABC as its ID is 1.


The Error will be raised when a user with ID 4 or other logged in, now as the default value it returns user id , here user id is 4 and there is no record with ID 4 in department table/model, so adjust your logic to get the default department ID and do accordingly.


Thanks

Avatar
Discard
Author Best Answer

i know this is wrong ( default=lambda self: self.env.uid)

but did not know why thanks i understand now

but don't know how to get department default value for user who created a record, i am beginner

Avatar
Discard
Related Posts Replies Views Activity
1
Nov 24
51
2
Sep 24
11248
1
Feb 19
5457
2
Mar 15
3782
0
Mar 15
3217