3

I Have data coming in to my model from an external source. The 'Log' model takes an employee ID, a log message and a timestamp. In some cases, a location ID (refering to res.partner, for a delivery address) is contained within the log message.

I use a python function to get the id out of the string, calling it from the 'compute' parameter in the field, which works fine. Now what I want to do, is to use that ID to link to res.partner, so that instead of the ID, the name of the location will show up in the table and will link to the right address.

class Log(models.Model):
_name = "MyModel.log"

employee_id = fields.Many2one('hr.employee', 'Employee')
timestamp = fields.Date('Timestamp')
message = fields.Char('Log message')
location_id = fields.Char(string='Location', compute='_get_location_id')

@api.one
@api.onchange('message')
def _get_location_id(self):
    searchedFor = "id = "
    stringToBeSearched = self.message
    result = stringToBeSearched.find(searchedFor)

    if result != -1:
        numbers = sum(c.isdigit() for c in stringToBeSearched)
        loc_id = stringToBeSearched[result + 5:result + 5 + numbers -1]
        self.location_id = loc_id
        print(loc_id)
    return

The problem is: When using a Char field, the ID shows up fine, but if the field type is changed to Many2One, nothing shows up, although the value is calculated correctly (I see the print showing up in the console). I know 'Many2One' fields don't take the 'compute' parameter, but then how should I do this?

Thanks in advance!

Arno
  • 305
  • 3
  • 14

0 Answers0