Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
5 ตอบกลับ
49435 มุมมอง

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)

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
มี.ค. 15
8819
Open form with current user record แก้ไขแล้ว
2
ก.ย. 21
10121
2
มิ.ย. 20
14237
4
พ.ย. 15
6468
1
มี.ค. 15
5100