Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
2175 Tampilan

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.

Avatar
Buang
Jawaban Terbai

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

Avatar
Buang
Penulis Jawaban Terbai

Like that?

Avatar
Buang
Jawaban Terbai

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

Avatar
Buang
Penulis

Hello,
What should be the dependency for the edit_recommended_field?

you can keep as non stored computed

Post Terkait Replies Tampilan Aktivitas
2
Jun 25
1284
0
Mar 25
1231
0
Feb 25
957
3
Agu 24
5600
1
Apr 24
33