Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
922 Vistas

I have 2 model: Contract & Room. 


In model room I have a selection field to set state for room. 


They are my model:


- Room:

	​
class PmaApartmentRoom(models.Model):    
​_inherit = "pma.apartment.room"
    contract = fields.One2many("pma.contract", "room", string="Contract")
    state = fields.Selection([
​("available", "Available"),
​("rented", "Rented")],
​string="Status", copy=False,
​index=True, readonly=True, store=True, tracking=True)
  class PmaContract(models.Model):   
​_name = "pma.contract"   
​_inherit = ["mail.thread", "mail.activity.mixin"]   
​_description = "Manage contract"
    state = fields.Selection([
​("draft", "Draft"),           
​("to_link", "To link"), 
        ​("active", "Active"),
        ("expired", "Expired"), 
        ("liquidated", "Liquidated")],       
​string="Status", default="draft", copy=False,       
​ index=True, readonly=True, store=True, tracking=True,)
   
​room = fields.Many2one("pma.apartment.room", string="Room name",        ​domain="[('apartment', '=', apartment)]",        ​ondelete="restrict", store=True, required=True, tracking=True)
State of contract = "active" => state of room = "rented".

State of contract in ["draft", "to_link", "expried", "liquidated"] => state of room = "available"


Please help me ....
Avatar
Descartar
Mejor respuesta

Hi Try this code,


class PmaApartmentRoom(models.Model):
    _inherit = "pma.apartment.room"

    # Add a computed field to calculate the room state based on associated contract state
    room_state = fields.Selection([
        ("available", "Available"),
        ("rented", "Rented")
    ], string="Room State", compute="_compute_room_state", store=True)

    @api.depends('contract.state')
    def _compute_room_state(self):
        for room in self:
            # Check the associated contract state and set room state accordingly
            if room.contract and room.contract.state == 'active':
                room.room_state = 'rented'
            else:
                room.room_state = 'available'

Hope it helps


Avatar
Descartar
Autor Mejor respuesta

Please help me ~~~

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
jul 19
13982
4
may 19
12322
1
jul 25
380
1
feb 25
1202
0
sept 23
2146