3

I have created three models: player, match and bet. A player can make N bets and a bet can be made by only one player (1:N). And a match can have N bets but a bet belongs to one match (1:N). So I have the following models:

class RWCPlayer(models.Model):
    _name = 'rwc.player'

    bet_ids = fields.One2many(
        comodel_name='rwc.bet',
        inverse_name='player_id',
        string='Bets',
    )


class RWCMatch(models.Model):
    _name = 'rwc.match'

    bet_ids = fields.One2many(
        comodel_name='rwc.bet',
        inverse_name='match_id',
        string='Bets',
    )


class RWCBet(models.Model):
    _name = 'rwc.bet'
    _order = 'date'

    player_id = fields.Many2one(
        comodel_name='rwc.player',
        string='Player',
        ondelete='cascade',
        required=True,
    )
    match_id = fields.Many2one(
        comodel_name='rwc.match',
        domain=[('state', '!=', 'finished')],
        string='Match',
        ondelete='cascade',
        required=True,
    )

    @api.onchange('match_id')
    def onchange_match_id(self):
        _logger.info(self)                      # LOGGER 1
        _logger.info(self._origin)              # LOGGER 2
        _logger.info(self.match_id)             # LOGGER 3
        _logger.info(self._origin.match_id)     # LOGGER 4
        _logger.info(self.player_id)            # LOGGER 5
        _logger.info(self._origin.player_id)    # LOGGER 6

The problem is the onchange method of the bet model (onchange_match_id).

If I open the bet with ID 350, for instance, and I click on the button Add an element of the one2many field bet_ids of player model, choose the player with ID 2 and afterwards modify the many2one of match, to select for example the match with ID 4, the logger throws this:

rwc.bet()                                                   # LOGGER 1
rwc.bet(<odoo.models.NewId object at 0x7fd7c0234990>,)      # LOGGER 2
rwc.match(4,)                                               # LOGGER 3
rwc.match()                                                 # LOGGER 4
rwc.player(<odoo.models.NewId object at 0x7f40f3f2f850>,)   # LOGGER 5
rwc.player()                                                # LOGGER 6

However, if I do exactly the same but from the one2many field bet_ids of match model:

rwc.bet()                                                   # LOGGER 1
rwc.bet(<odoo.models.NewId object at 0x7fd7c0234990>,)      # LOGGER 2
rwc.match(<odoo.models.NewId object at 0x7fd7c0234b50>,)    # LOGGER 3
rwc.match()                                                 # LOGGER 4
rwc.player(2,)                                              # LOGGER 5
rwc.player()                                                # LOGGER 6

So my question is: how can I get the recordset of the many2one of player_id if I have opened the bet form from the one2many bet_ids of model player? And the same, how can I get the recordset of the many2one of match_id if I have opened the bet form from the one2many bet_ids of model match?

@CZoellner helped me here with a similar problem, but the behaviour of this case seems to be different. Any help will be appreciated.

forvas
  • 9,801
  • 7
  • 62
  • 158
  • Very interesting question! Maybe you should post it in the Odoo forums, too. I would try to ping rco-odoo there, if it's possible (didn't use it so much so far). – CZoellner Jun 14 '18 at 18:23

0 Answers0