Skip to Content
Menu
This question has been flagged
3 Replies
1282 Views

Hello everyone,


I'm trying to fill in a form field with the reference of the user connected to the site.


Part 1 :

I've created a "car" data model with a "owner_id" many2one field inside.

When the logged-in user complete the form to have a new car (with name, price, etc.), I'd like to fill his "ref_user" automatically in a field of the form.


Part 2 :

I've add the following code line in parameters-> technical -> Models -> my car model -> the owner_id field -> advanced properties -> compute

"owner_id = fields.Many2one('res.users', default=lambda self: self.env.user)"


Part 3
I have this error : 

: "name 'fields' is not defined" while evaluating
"owner_id= fields.Many2one('res.users', default=lambda self: self.env.user)"

It's like the compute don't authorize the fields object...
I try to just do "owner_id = self.env.user" but it doesn't seems to work and i can't have a "console.log" of the owner_id.
How can i fix this error ?

Answer : 
In the compute field, the code needed is :

for record in self:

    record ['x_studio_owner_id'] = record.env.user



Thanks for your help.

Avatar
Discard
Best Answer

Hi,
You can define the field and set default value as current user by below code:

user_id = fields.Many2one('res.users', default=lambda self: self.env.user)



Thanks

Avatar
Discard
Author Best Answer

Thanks for you answer all. 
With what you gave my I finally found how to do it.

In the compute field, the code needed is :
for record in self:

    record ['x_studio_owner_id'] = record.env.user

Avatar
Discard
Best Answer

if you need to know the created user you can use the magic field create_uid this field is filled automatically with logged in user.

Avatar
Discard
Related Posts Replies Views Activity
0
Mar 24
3507
2
Aug 24
1611
2
Jan 25
87
0
Jan 24
804
3
Sep 23
1619