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

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
Buang

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

Penulis

@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

Jawaban Terbai

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
Buang
Penulis

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.

Jawaban Terbai

Hello Learning_Odoo,

Please find code in comment.

I hope This will help you.

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

Avatar
Buang

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

Jawaban Terbai

Use this attribute in your field/button.

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

Avatar
Buang
Post Terkait Replies Tampilan Aktivitas
0
Nov 23
1336
2
Agu 16
5388
2
Jun 25
1772
0
Agu 24
2114
1
Agu 24
1770