Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
4220 Widoki

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

Awatar
Odrzuć

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

Autor

@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

Najlepsza odpowiedź

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.


Awatar
Odrzuć
Autor

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.

Najlepsza odpowiedź

Hello Learning_Odoo,

Please find code in comment.

I hope This will help you.

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

Awatar
Odrzuć

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

Najlepsza odpowiedź

Use this attribute in your field/button.

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
lis 23
1336
2
sie 16
5386
2
cze 25
1771
0
sie 24
2112
1
sie 24
1770