2

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?

forvas
  • 9,801
  • 7
  • 62
  • 158
  • Show us how you define those fields. – George Daramouskas Sep 04 '18 at 12:40
  • 2
    Problem here is that the compute method is called before the rendering of the view, so there won't be any context from the view on computation. I've struggeled about this so often, but never found a solution, without doing some dirty things. – CZoellner Sep 04 '18 at 12:41
  • Do you want to be able to select 2 different dogs? Or do you want to display 2 different fields of the same referenced dog? – miw Sep 04 '18 at 17:57
  • @miw I want to be able to select two different dogs (but I could select the same dog in both fields). However, in one field I want to see names and in the other aliases. – forvas Sep 05 '18 at 08:50
  • @GeorgeDaramouskas I've updated the Python code to show the definition of both fields. – forvas Sep 05 '18 at 08:55

0 Answers0