This question has been flagged
3 Replies
8533 Views

I have a module with 3 fields:

  • name = fields.Char(...)

  • description = fields.Char(...)

  • forbridden_user = fields.Many2one('res.user',...)

When I create a record in field forbridden_user I select user which I want prevent from editing field description. How could I achieve that? I tried doing the fallowing:

  • <field name="description" attrs="{'readonly': [('forbidden_user', '=', 'uid')]}"/>

But it doesn't work. 

  • <field name="description" attrs="{'readonly': [('forbidden_user', '=', uid)]}"/>

Causes error:

  • Error details: "name 'uid' is not defined" while evaluating

Avatar
Discard
Best Answer

First, you have to replace the uid by a function field that returns the current login id.

You have also to put "!=" instead of "="

so your line will be 

<field name="description" attrs="{'readonly': [('forbidden_user', '=', uid)]}"/>

But even if you did that, you won't be able to access the field 'description' in 'create' mode.

==========

I think you have to create a group of the users that allowed to see this field, or the converse if you wish, and create a function field that returns true if the current user is in the specified group. Then based on this function field you can set the 'readonly' attribute

Pls check this link and the links similar it.


Note :

I used the uid in defining domain of an action in the xml file. I tried to put the uid in the attributes definition of a field in the xml in the view definition and I got the same error as yours, so I think you have to try the 2nd solution I posted.

Avatar
Discard
Best Answer

Hi,

Just make your field defination inside xml as like below.

<field name="description" attrs="{'readonly': [('forbidden_user', '=', uid)]}"/>

Here you need to write UID as variable not as text.

I hope it will resolve your issue.

Thanks.

Avatar
Discard