コンテンツへスキップ
メニュー
この質問にフラグが付けられました
3 返信
2178 ビュー

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
1289
0
3月 25
1237
0
2月 25
957
3
8月 24
5606
1
4月 24
33