Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How can i get logged user name automatically into field ?

By
Dr Obx
on 9/29/15, 8:12 AM 591 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 ?


1
Juan Muñoz
On 9/29/15, 10:23 AM

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.

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 ?

Dr Obx
on 9/29/15, 10:48 AM

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

Dr Obx
on 9/29/15, 10:50 AM

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

Dr Obx
on 9/29/15, 10:51 AM

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

Juan Muñoz
on 9/29/15, 10:59 AM

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 ?
Dr Obx
on 9/30/15, 6:31 AM

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 :(

Dr Obx
on 9/30/15, 7:45 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/29/15, 8:12 AM
Seen: 591 times
Last updated: 9/29/15, 10:23 AM