콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
1329 화면

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.

아바타
취소
베스트 답변

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

아바타
취소
작성자 베스트 답변

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

아바타
취소
베스트 답변

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.

아바타
취소
관련 게시물 답글 화면 활동
0
3월 24
3523
2
8월 24
1652
2
1월 25
87
0
1월 24
836
3
9월 23
1655