Skip to Content
Menu
This question has been flagged
5 Replies
8058 Views

how to get current user id in a field in Odoo online version?

Avatar
Discard

Get current logged in user id: https://goo.gl/KzXLak

Author Best Answer

I tried to solve it by myself. Thanks for all your replies. 

Avatar
Discard
Best Answer

Hello Techy Mills,

For an online version, you can use the Automated Actions for this.

Activate Developer mode and Go to Settings -> Technical -> Automation -> Automated Actions

When creating an Automated action, you have the option to select the

1. Related Document Model.

2. When to Run

Here you have Option In the When to Run field to select when the operation should be executed.

Like: On Creation, On Update, Based on form Modification, etc.

If you want the field the value as default, then go for Based on form Modification 

1. Value as Default

--------------------------------------------

Based on form Modification. It is same as the onchange method. Here we have to apply a trick to get the value as default.

What is the trick? I will explain, Create a new dummy field and trigger the onchange method based on that Field. and hide that field from the view.  then this onchange method will call only at the time of creation. (we can't change the value of that field because it is hidden from view. so it will call the onchange method only one time)

Next, there is Actions Tab in the Automated Actions. under that Tab, you have Server actions to run field.

Create a server action. Select Action To Do field as Execute Python Code. The Base Model field is the same model.

In the python code, put like this

record['x_your_user_field'] = env.user.id

This is the way to achieve "Value as default".

--------------------

On Creation and On Update is more easy.

Here select When to Run as On Creation & On Update respectively. No need for Dummy field. Next steps are the same as the first way(Onchange)



Thanks & Regards


Avinash N K
Avatar
Discard
Best Answer

Hi,

You can get the current user by this code,

user_id = fields.Many2one('res.users', 'Me', default=lambda self: self.env.user)

Thanks

Avatar
Discard
Author

I cannot access the model file in odoo online. Any other idea?