Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odpovědi
2170 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Autor Nejlepší odpověď

Like that?

Avatar
Zrušit
Nejlepší odpověď

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
Zrušit
Autor

Hello,
What should be the dependency for the edit_recommended_field?

you can keep as non stored computed

Related Posts Odpovědi Zobrazení Aktivita
2
čvn 25
1275
0
bře 25
1228
0
úno 25
956
3
srp 24
5596
1
dub 24
33