our issue seems to stem from how the context is being set and accessed in your custom Odoo method. Let’s break it down and identify why the context value (bra_class) might not be retrieved as expected.
Issues in Your Code:
1. Context Setting on self:
self = self.sudo().with_context(bra_class=branch_cate)
You’re modifying self to include the context, but this doesn’t actually persist the new context to the method. The self reference is local to this method and does not affect the surrounding environment or the call stack.
2. Accessing Context with self._context:
- Later in the method, when you attempt to retrieve the value using:
bra_class_value = self._context.get('bra_class', 'لايوجد')
The _context attribute on self refers to the original context of the method call, not the modified context you attempted to set with with_context.
3. Improper Use of with_context:
- The with_context method returns a new recordset with the added context, but it does not retroactively apply that context to the method already in progress.
Suggested Fix:
Modify your approach to work directly with the context passed to the fields_view_get method. Here’s the corrected code:
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
# Call the super method to get the base view structure
res = super(HrEmployeePrivate, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)
if view_type == 'form':
# Get branch category and add it to the context
branch_cate = self.branch_name_id.branch_cate if self.branch_name_id else "tttttt"
# Add branch category to the context
ctx = dict(self._context, bra_class=branch_cate)
# Retrieve the value from the updated context
bra_class_value = ctx.get('bra_class', 'لايوجد') # Default to 'لايوجد' if not found
print("Context Value for 'bra_class':", bra_class_value)
# Iterate through fields in the view
for field_name, field_data in res.get('fields', {}).items():
if field_name == 'job_ids':
# Dynamically set the domain for the field
field_data['domain'] = [
'|',
('company_id', '=', False),
('company_id', '=', self.env.user.company_id.id),
('bra_class', '=', bra_class_value) # Use the context value dynamically
]
return res
Key Changes:
1. ctx = dict(self._context, bra_class=branch_cate)
- This creates a new dictionary by copying the current context and adding the bra_class key.
2. Access Context via the Updated ctx:
- Instead of attempting to modify self, you work with the ctx variable and extract bra_class from it.
3. Use ctx for Further Logic:
- The updated context is used to dynamically set the domain for job_ids.
Debugging Tips:
1. Ensure branch_name_id is Set:
- Add a check to verify that branch_name_id is populated; otherwise, branch_cate will default to “tttttt”.
2. Log the Context:
- Use print or logging to confirm the structure of the context before and after modifications.
3. Validate Domains:
- Test the resulting domain to ensure it’s applied correctly in the form view.