Skip to Content
Menu
This question has been flagged
6 Replies
9607 Zobrazenia

I want to make the field A readonly for the customer group which is a new group i have created. 

I tried this but it didnt work. Is there another way to do this or a way to keep track of the logged in user in the xml view?

attrs="{'readonly': [('groups', 'in', [crm.customer_group])]}"">


Avatar
Zrušiť
Autor

I have tried creating a boolean field and using it to make fields read only but there is no way for my python file to tell the boolean field in xml to become true based on user login.

I tried @Cybrosys Tech method but that doesn't render the field for any other group just the one mentioned in the groups tag. Anyway to dynamically change the readonly for field A based on loggedIn user's group?

Best Answer

define boolean compute filed on model and user attrs on view to make field readonly

ref: https://github.com/odoo/odoo/blob/16.0/addons/hr_holidays/models/hr_leave_allocation.py#L134

Avatar
Zrušiť
Best Answer

Hi,

Use the attribute <t groups />

Try this example

<t groups="crm.customer_group">

<field name="field_name" readonly="1"/>

</t>

Hope it helps

Avatar
Zrušiť
Best Answer

The solution I found is duplicating the field and set specifics groups for each case.

This is a example:




field name="can_edit" invisible="1"/>

field name="resolucion" attrs="{'readonly':[('can_edit', '!=', True)]}"/>


@api.depends('resolucion')

    def _compute_can_edit(self):

        for record in self:

            if self.env.user.has_group('interturnos.group_interturnos_resolucion_modifier'):

                record.can_edit = True

            else:

                record.can_edit = False


        

Avatar
Zrušiť
Best Answer
def _compute_my_field_readonly(self):
return not (
self.user.has_group("my_module.group_1")
or self.user.has_group("my_module.group_2")
)

my_field = fields.Float(
readonly=_compute_my_field_readonly,
)


Avatar
Zrušiť
Best Answer

Hi Walaga Qapas,

Also, you can  do this by using a boolean field...

for example --- step1 - suppose we create a boolean field called "boolean_check"

​step2 - write a compute method and set "boolean_check" field True/False base on the login user.

​step3 - write attrs attribute in the field that you want to make readonly.

  ​condition like- attrs="{'readonly': [('boolean_check', '=' ,True)]}"

 

Avatar
Zrušiť
Best Answer

Hi  Walaga Qapas,

Try,

id="employee_normal_user_uneditable" model="ir.ui.view">
name="name">hr.employee.ta
name="model">hr.employee
name="inherit_id" ref="hr.view_employee_form"/>
name="groups_id" eval="[(6, 0, [ref('module_name.group_name')])]"/>
name="arch" type="xml">
name="field_name1" position="attributes">
name="readonly">1

name="field_name2" position="attributes">
name="readonly">1




Avatar
Zrušiť

<record id="employee_normal_user_uneditable" model="ir.ui.view">
<field name="name">hr.employee.ta</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="groups_id" eval="[(6, 0, [ref('module_name.group_name')])]"/>
<field name="arch" type="xml">
<field name="field_name1" position="attributes">
<attribute name="readonly">1</attribute>
</field>
<field name="field_name2" position="attributes">
<attribute name="readonly">1</attribute>
</field>
</field>
</record>

Related Posts Replies Zobrazenia Aktivita
1
sep 23
1869
1
mar 15
7211
1
júl 23
2247
2
máj 23
5885
1
máj 23
218