Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
5 Antworten
49012 Ansichten

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?

Avatar
Verwerfen
Beste Antwort

This is so short for getting the current user info:

self.env.user

Avatar
Verwerfen
Beste Antwort

@Miftah

under the _defaults you can write the following code

_defaults={

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

}

hope this helps !!

Avatar
Verwerfen

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 )

Beste Antwort

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.

Avatar
Verwerfen

thank you very much

Beste Antwort

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.

Avatar
Verwerfen
Beste Antwort

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

Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
1
März 15
8654
2
Sept. 21
9933
2
Juni 20
14008
4
Nov. 15
6223
1
März 15
4967