records on list view or tree view that relates to active/login user on the click of menu. For example for admin user I want to show all records, but for employee I want to show selective records that are related to that employee.
Server Action in Odoo (ir.actions.server)
To use server action on the click of menu follow below steps.1- Create server action and call that action from your menu.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<record id="server_action_any_name" model="ir.actions.server"> | |
<field name="name">Any Name Goes Here</field> | |
<field name="condition">True</field> | |
<field name="type">ir.actions.server</field> | |
<field name="model_id" ref="model_your_model_name" /> | |
<field name="state">code</field> | |
<field name="code">action=self.get_filtered_record(cr, uid, context.get('active_ids', []), context=context)</field> | |
</record> | |
<menuitem name="Your Menu Name" id="menu_any_name" action="server_action_any_name"/> |
2- After that create a method or function under your model, and from that model after using your logic return action window.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@api.multi | |
def get_filtered_record(self): | |
if self._context.get('params', False): | |
params = self._context.get('params', False) | |
if params.get('menu_id', False): | |
raise ValidationError( | |
"Attention:You are not allowed to access this page due to Security Policy. In case of any query, please contact ERP Admin or Configuration Manager.") | |
else: | |
return False | |
view_id_form = self.env['ir.ui.view'].search([('name', '=', 'your.form.view.name')]) | |
view_id_tree = self.env['ir.ui.view'].search([('name', '=', 'your.tree.view.name')]) | |
group_pool = self.env['res.groups'] | |
user = self.env['res.users'].browse(self._uid) | |
employee_pool = self.env['hr.employee'] | |
employee = employee_pool.search([('user_id', '=', user.id)]) | |
if user.has_group('base.group_erp_manager'): | |
record_ids = self.env['your.model.name'].search([]).ids | |
if user.has_group('base.group_hr_user'): | |
record_ids = self.env['your.model.name'].search([('company_id', '=', employee.company_id.id)]).ids | |
else: | |
record_ids = employee.ids | |
return { | |
'type': 'ir.actions.act_window', | |
# 'name': _('Product'), | |
'res_model': 'your.model.name', | |
'view_type': 'form', | |
'view_mode': 'tree,form', | |
# 'view_id': view_id_tree.id, | |
'views': [(view_id_tree.id, 'tree'), (view_id_form.id, 'form')], | |
'target': 'current', | |
'domain': [('id', 'in', record_ids)] | |
# 'res_id': your.model.id, | |
} |
Don,t forgot to share, keep sharing keep learning
2 Comments
ValueError: : "name 'self' is not defined" while evaluating
ReplyDelete"action=self.get_filtered_record(cr, uid, context.get('active_ids', []), context=context)"
on odoo12 complains that it cannot find 'self'
ideas what may be wrong?
should be model instead of self, I guess, see: https://www.odoo.com/documentation/12.0/reference/actions.html
ReplyDelete