Skip to Content
Menu
This question has been flagged
6 Replies
10012 Views

Hi,

I'm trying to set a default value for the field create_uid in my python file. After research on the forum I found that the correct way to do this should be something like:

create_uid=fields.Many2one(default=lambda self: self.env.user)

I've tried different syntaxes but at best it returns "1"  (since I'm the first user as administrator) but it should return "Administrator".

Thank you in advance for any help.

Avatar
Discard
Best Answer

Hi Mehdi, why do you need to add create_uid field.Its default odoo field which will save with the id of the current logged in user when the record has been created.Just you need to add theis field to your views to see it.

Eventhough, if you want to add another field for some purpose which should work like create_uid,you can implement it like,

create_uid=fields.Many2one('res.users',default=lambda self: self.env.user and self.env.user.id or False)

I will advice you to take different name for the field other than create_uid.

Avatar
Discard
Author

Hi Raaj thank you for your answer but what I'm trying to do is to override the field create_uid so that when displayed in form view; the user can't choose another user in the Many2one widget. If I do this with readonly attribute, neither the widget nor the user name is displayed.

If possible can you pls post the screenshot.

One more thing, at you overridden field definition, you have missed to pass the object name i.e., res.users, which i have corrected and included in my answer.

Author

Sorry I don't succeed in posting pictures on the forum but you can access it between url tags. I've tried your sample but I don't understand why you use "and" & "or" and I can't compile your code.