コンテンツへスキップ
メニュー
この質問にフラグが付けられました
5 返信
48970 ビュー

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?

アバター
破棄
最善の回答

This is so short for getting the current user info:

self.env.user

アバター
破棄
最善の回答

@Miftah

under the _defaults you can write the following code

_defaults={

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

}

hope this helps !!

アバター
破棄

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 )

最善の回答

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.

アバター
破棄

thank you very much

最善の回答

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.

アバター
破棄
最善の回答

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

アバター
破棄
関連投稿 返信 ビュー 活動
1
3月 15
8635
2
9月 21
9879
2
6月 20
13995
4
11月 15
6212
1
3月 15
4962