Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
1832 Lượt xem

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 3 24
3942
2
thg 8 24
2336
2
thg 1 25
87
0
thg 1 24
1162
3
thg 9 23
2174