Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odgovori
4217 Prikazi

I want to hide some buttons based on a checkbox that is in res.users odoo since I have divided the users by that checkbox... the users who have the checkbox activated are "admin" and those who do not have it activated are normal users, I want to hide the buttons from the users who do not have the checkbox activated... how can I do this? Odoo 14 community.

Thanks

Avatar
Opusti

Do you want to show/hide the buttons/fields in res.users view or different object's view?

Avtor

@Sudhir I want to hide the buttons that are in another model and in another view, but based on the value of a checkbox field that is in res.users

Best Answer

If you want to show/hide the buttons in res.users view, you can use attrs as "Sehrish" suggested. 

But if you want to do it in a different object's view, you need to override fields_view_get method in that object and show/hide the buttons based on the currently logged-in user.

Use the following code in your object:

from lxml import etree

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):

result = super(MyClass, self).fields_view_get(
view_id, view_type, toolbar=toolbar, submenu=submenu)
doc = etree.XML(result['arch'])
hide = True
if self.env.user.my_boolean:
hide = False
for node in doc.xpath("//button[@name='my_button']"):
modifiers = json.loads(node.get("modifiers", '{}'))
modifiers.update({'invisible': hide})
node.set("modifiers", json.dumps(modifiers))
result['arch'] = etree.tostring(doc)
return result

This code you can use in any object to show/hide the buttons/fields/groups/tab, etc.


Avatar
Opusti
Avtor

Thank you...
but my plan was as follows:
In the res.users model I have a checkbox field to identify a type of user...
The buttons that I want to hide are not in res.users or in the view, it is a very different model than res.users... I imagined that I would have to do a Many2one of the model where I have the buttons to res.users and from there try make a related with that field... and then put a condition to the buttons that if the checkbox is disabled, hide the buttons hehe

The above code will work in any object. You will just have to put this method in your object and everything will work as you expect.

Best Answer

Hello Learning_Odoo,

Please find code in comment.

I hope This will help you.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Opusti

You can used attributes for based on state field.
<button attrs="{'invisible': [('state', '=', 'click_true/false')]}" groups="group_id"/>

Best Answer

Use this attribute in your field/button.

attrs="{'invisible':[('your_checkbox_field','=',False)]}"

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
0
nov. 23
1336
2
avg. 16
5384
2
jun. 25
1770
0
avg. 24
2110
1
avg. 24
1768