Skip to Content
Menu
This question has been flagged
2 Replies
920 Views

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
Discard
Best Answer

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
Discard
Author Best Answer

Please help me ~~~

Avatar
Discard
Related Posts Replies Views Activity
1
Jul 19
13979
4
May 19
12316
1
Jul 25
379
1
Feb 25
1202
0
Sep 23
2144