I am going to put a simple example:
I have two Many2one fields, dog_name
, dog_alias
, both of them pointing to the dog
model. This model has the Char fields name
and alias
.
When I open the dropdown of dog_name
, I should see the name of the dogs, when I open the dropdown of dog_alias
, I shouls see the alias of the dogs.
For instance, I have a record of dog
with ID 57, name Fox and alias Foxito, if I open the dropdown of dog_name
, I should see Fox, if I open the dropdown of dog_alias
, I should see Foxito. But if I select any of them, the record selected will be the same (the one with ID 57).
To manage this behaviour, I always used name_get
combined with a XML context:
odoo - display name of many2one field combination of 2 fields
However, this time the following is happening: when I deploy the dog_alias
dropdown, I see Foxito, which works great, but when I save the record, I see Fox. I guess because that is the display_name
of the model, but if I do this:
Python
@api.multi
@api.depends('name', 'alias')
def _compute_display_name(self):
for dog in self:
if self._context.get('display_name', False) == 'alias':
dog.display_name = dog.alias
else:
dog.display_name = dog.name
display_name = fields.Char(
compute='_compute_display_name',
)
dog_name = fields.Many2one(
comodel_name='dog',
string='Dog Names',
required=True
)
dog_alias = fields.Many2one(
comodel_name='dog',
string='Dog Aliases'
)
XML
<field name="dog_name"/>
<field name="dog_alias" context="{'display_name': 'alias'}"/>
The problem is still the same. The context
in the _compute_display_name
method never brings the one I set in the XML file, therefore, if I select Foxito in the dog_alias
field, after saving I see Fox in that field, which is not what I want.
So, do you have any idea of how to solve this situation?