Showing intended field at frontend

I have a class as below:

class Employee(ModelSQL, ModelView):
    "Employee"
    __name__ = "afx.employee"
    _rec_name = "name"

    employee_id = fields.Char("Employee No", required=True)
    name = fields.Char("Name", required=True)

And another class that use above class:

class ProjectSales(ModelSQL, ModelView):
    __name__ = 'afx.crm.sales'

    employee_no = fields.Many2One('afx.employee', 'Employee No', required=True)
    name = fields.Char('Name', required=True, states={
        'readonly': True,
    },)

    @fields.depends('employee_no')
    def on_change_with_name(self, name=None):
        if self.employee_no:
            return self.employee_no.name
        return ''

Since the _rec_name of Employee class pointing to ‘name’ field, and already use by many other classes, all front-end of those classes displays employee’s name in the front end.

Problem: currently, the frontend that use ProjectSales class displays the employee’s name too. while it is required is to show the employee_id of the Employee object instead of following the _rec_name setup.

How to show the employee_id value at ProjectSales front-end?

Bromo

We do not have yet a clean way to request that a Many2One should display another field than rec_name in the UI. See Use field context to read .rec_name (#8227) · Issues · Tryton / Tryton · GitLab
But you could customize the method get_rec_name to behave differently based on the context. And so you could set a key in the context of the Many2One on ProjectSales that will change the behavior of get_rec_name.

Side note:

This could be a Function field which would be kept always in sync with the employee name.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.