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

I have a field many2one relation to res.users like this : 'salesman':fields.many2one('res.users', string='Salesman').

I want default value it field is current user ID (current login) like Administrator, Custom User, etc. How to do it?

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

This is so short for getting the current user info:

self.env.user

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

@Miftah

under the _defaults you can write the following code

_defaults={

'salesman':lambda self, cr, uid, ctx=None: uid

}

hope this helps !!

Ảnh đại diện
Huỷ bỏ

In the 8.0 API this would be something like this: salesman = fields.Many2one( string="Salesman", comodel_name='res.users', default= lambda self: self.env.user.id )

Câu trả lời hay nhất

Hello,

You can use below code.

context = self._context

current_uid = context.get('uid')

user = self.env['res.users'].browse(current_uid)

Hope this help you.

Best Thanks,

Ankit H Gandhi.

Ảnh đại diện
Huỷ bỏ

thank you very much

Câu trả lời hay nhất

Why don't you take a look at sale.order model from odoo/addons/sale/sale.py.  It has user_id defined which - AFAIK - mimics what you need.  It uses _default as well as @Emipro had suggested.

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

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 3 15
8627
2
thg 9 21
9873
2
thg 6 20
13994
4
thg 11 15
6210
1
thg 3 15
4961