Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
5 Odpovědi
48749 Zobrazení

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
Zrušit
Nejlepší odpověď

This is so short for getting the current user info:

self.env.user

Avatar
Zrušit
Nejlepší odpověď

@Miftah

under the _defaults you can write the following code

_defaults={

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

}

hope this helps !!

Avatar
Zrušit

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 )

Nejlepší odpověď

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
Zrušit

thank you very much

Nejlepší odpověď

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
Zrušit
Nejlepší odpověď

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

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
bře 15
8569
2
zář 21
9800
2
čvn 20
13863
4
lis 15
6097
1
bře 15
4847