Skip to Content
Menu
This question has been flagged
1 Reply
6653 Views

Guys,

How can I get user name (like create_uid which automatically sets up user logged in as a default value) in custom field ?


Avatar
Discard
Best Answer

Hi Dr Obx,

If you see, when creates an invoice, the sales person field is completed automatically by the user logged. 

Then, you should go to your attribute in the python file and put this: 

you_field_name= fields.Many2one('res.users', string='your_string_field', track_visibility='onchange',

readonly=True, states={'draft': [('readonly', False)]}, default=lambda self: self.env.user)

-If you want to see more you can check it in /addons/account/account_invoice.py in line 304 (attribute user_id).


Hope this answer helps you! ^^

Juan.

Avatar
Discard
Author

Yes, good point. Why I did not checked how it works in other modules instead :) Silly Me :) The question is, why Once I add default=lambda self: self.env.user to the field definition in py and trying upload CSV file immediately it gives me an error ?

Author

Unknown error during import: : Wrong value for iprodstep.log.employee_id: res.users(1,) at row 2 Resolve other errors first

Author

However none of those fields not exist in CSV so it's not loaded...

Can you copy the piece of code that you think may fail here please? I do not know if I can help you more but I'll try. Try putting only: self.env.user

Author

Hi Juan, I just added "default=lambda self: self.env.user" into the line so it is like that:

...
	employee_id = fields.Many2one('hr.employee', string='Entered by', required=False, default=lambda self: self.env.user )
	technician_id = fields.Many2one('hr.employee', string='Technician', required=False, default=lambda self: self.env.user)
	packing_id = fields.Many2one('hr.employee', string='Packed by', required=False, default=lambda self: self.env.user)
	warehouse_id = fields.Many2one('hr.employee', string='Picked by', required=False, default=lambda self: self.env.user)
...
I did not even touch "import" procedure so why it generate error when I'm trying to validate CSV before loading ? I think I know. Maybe the problem can be solved other way. I can create a separate class which will include employee, technician, packing and warehouse then inherit in main/parent class ?
Author

As I said, created separate class, inherited by main class but it still won't work. Still getting error whilst I'm loading data from CSV :( so it doesn't help :(