跳至內容
選單
此問題已被標幟
3 回覆
2208 瀏覽次數

Hello,

I have 3 companies (for e.g. Company 1,2 and 3) and I have created a field Recommended price in all of them. However I want to be able to update the field only if the user is an Admin in Company 1, else it is read-only. How can I achieve that?

Thanks.

頭像
捨棄
最佳答案

Hi Fadil,


To achieve the requirement of making the recommended_price field editable only for administrators of Company 1, you can use the attrs attribute in the XML view definition to control the read-only state of the field based on the user's group membership and the company. Here's how you can implement this:

       

  1.        

    Create a new group (e.g., company_1_admin) in your custom module for administrators of Company 1.

       

  2.    

  3.        

    In your custom module's XML file (e.g., views/your_model_view.xml), locate the <field> element for the recommended_price field in the form view.

       

  4.    

  5.        

    Add the attrs attribute to the <field> element with the following value:

       

<field name="recommended_price" 

    attrs="{'readonly': ['|', ('user_has_groups', 'not in', ['your_module.company_1_admin']), ('company_id', '!=', 1)]}" />

Replace 'your_module.company_1_admin' with the actual XML ID of the company_1_admin group you created, and 1 with the ID of Company 1.

This expression will make the recommended_price field read-only if:

       

  • The current user does not belong to the company_1_admin group (user_has_groups, 'not in', ['your_module.company_1_admin']), OR
  •    

  • The current record's company_id is not equal to 1 (company_id, '!=', 1)

Hope it helps


Hope it helps

頭像
捨棄
作者 最佳答案

Like that?

頭像
捨棄
最佳答案

Hi,
Create a computed boolean field in this model, lets say, edit_recommended_price and compute the value to this field, if logged in user is admin, set this value as True for the field and in the view level, using the attrs, make the field editable only if edit_recommended_price is true

Thanks

頭像
捨棄
作者

Hello,
What should be the dependency for the edit_recommended_field?

you can keep as non stored computed

相關帖文 回覆 瀏覽次數 活動
2
6月 25
1343
0
3月 25
1295
0
2月 25
1023
3
8月 24
5675
1
4月 24
33