Skip to Content
Menu
This question has been flagged

Hi all, I have a custom new module in odoo 17 , I want to set 'work_email' field set as read only for employees, the work_email field is already have in hr module (res_users.py) and it the employee user profile it have a many field so i want to customize so i tried create a new group in sales employee category the name is sales executive . in hr group it alreadery have two groups are admin and officer. these are have full access in the profile . if the normal employees go to the profile , they are only have read mode option if they are not have any group selected in the hr category. so i want to try set full access by using officer group from hr and i add additionally sales executive group in the employee users , and i now i want to declare if the user has sales executive group , the work email only want to go read only so How to achieve this task?  

my codes: module : custom_sales_module models:custom_res_users.py: ```class CustomUserProfile(models.Model): _inherit = 'res.users' ``` security/ir_model_group : ``` Sales Role 10 Sales Executive ``` views: custom_res_users_views: ``` custom.user.profile.security.view res.users 1 ``` but by this code ,this field not set readonly for sales executive group it set globally. so check my code and give correct code:

Avatar
Discard
Best Answer

Hi,

Please check the code below:

Group for sale executive:

 <record id="sales_executive_group" model="res.groups">
<field name="name">Sales Executive</field>
</record>


Python:

from odoo import fields, models


class HrEmployee(models.Model):
_inherit = 'hr.employee'

is_sales_executive = fields.Boolean(string="Is Sale Executive",
compute="_compute_is_sales_executive")

def is_sale_executive(self):
"""
Compute method to determine whether the current user belongs
to the 'sales_executive_group'. Sets 'is_sales_executive' to True
if the user has the group, otherwise False."""
for rec in self:
if self.env.user.has_group('Module_Name.sales_executive_group'):
rec.is_sale_executive = True
else:
rec.is_sale_executive = False

XML

<record id="view_employee_form" model="ir.ui.view">
<field name="name">hr.employee.view.form</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<field name="work_phone" position="after">
<field name="is_sales_executive" invisible="1"/>
</field>
<field name="work_email" position='attributes'>
<attribute name="readonly">is_sales_executive</attribute>
</field>
</field>
</record>


Hope it helps.

Avatar
Discard
Related Posts Replies Views Activity
0
Jun 24
838
1
Jun 23
3041
2
Jun 25
1610
0
Aug 24
1705
2
Jul 24
1627