0
votes

I'm getting the following error trying to filter a field by another field value of the same model.

File "/opt/..../tfutbol/models/partido.py", line 29, in Partido figura = fields.Many2one('tfutbol.jugador',domain=[('equipo_id','=',local.id)]) RuntimeError: maximum recursion depth exceeded while calling a Python object

The line of code trying with the problem is:

figura = fields.Many2one('tfutbol.jugador',domain=[('equipo_id','=',local.id),('equipo_id','=',visitante.id)])

All the relevant code, is above:

class Partido(models.Model):
    _name = 'tfutbol.partido'

    local = fields.Many2one('tfutbol.equipo')
    visitante = fields.Many2one('tfutbol.equipo')
    figura = fields.Many2one('tfutbol.jugador',domain=[('equipo_id','=',local.id),('equipo_id','=',visitante.id)])

class Equipo(models.Model):
    _name = 'tfutbol.equipo' 

    name = fields.Char('Nombre')

    jugador_ids = fields.One2many('tfutbol.jugador', 'equipo_id', string="Jugadores")

class Jugador(models.Model):
    _name = 'tfutbol.jugador'

    name = fields.Char('Nombre')
    equipo_id = fields.Many2one('tfutbol.equipo')

Thanks for reading!

1

1 Answers

0
votes

If you read the docstring on /odoo/fields.py on the Many2one class definition you will see that:

:param domain: an optional domain to set on candidate values on the client side (domain or string)

That means that you cannot use the dot notation (record.field) to pull in values because this has not been implemented on javascript.

So what you can do to dynamically create a domain like the one you want is:

Create an onchange method that will be invoked every time you set a value on the local and visitante fields and will change the domain on the figura field. For example:

@api.onchange('figura','visitante')
def onchange_method(self):
      domain = {}
      domain['figura'] = [('equipo_id','=',self.local.id),('equipo_id','=',self.visitante.id)]
   return {'domain': domain}

Note: When you do not set an operator in a domain with multiple leafs (parameters) an explicit AND is added, so in your domain you are searching for the equipo_id filed to be equal to self.local.id AND self.visitante.id which will not work, you might want to add an OR like:

['|',('equipo_id','=',self.local.id),('equipo_id','=',self.visitante.id)]