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

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

アバター
破棄

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

著作者

@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

最善の回答

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.


アバター
破棄
著作者

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.

最善の回答

Hello Learning_Odoo,

Please find code in comment.

I hope This will help you.

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

アバター
破棄

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

最善の回答

Use this attribute in your field/button.

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

アバター
破棄
関連投稿 返信 ビュー 活動
0
11月 23
1336
2
8月 16
5391
2
6月 25
1778
0
8月 24
2119
1
8月 24
1785