pythonodoo

How to get a field from another model in odoo


I want to access the seats_number field from the plane model when the name field in the plane model is equal to the plane_id firld from the flight model

@api.depends('plane_id')
def _compute_seats_number(self):
    for flight in self:
        for plane in self.env['airport.plane'].search([]):
            if flight.plane_id == plane.name:
                flight.seats_number = plane.seats_number
                break            

Solution

  • What about using a related field instead?

    class FlightModel(models.Model):
        # usual stuff
    
        plane_id = fields.Many2one(
            comodel_name="my.plane.model",
            string="Plane",
        )
        seats_number = fields.Integer(
            related="plane_id.seats_number",
        )