Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
1014 Lượt xem

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 ....
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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


Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Please help me ~~~

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 7 19
14113
4
thg 5 19
12482
1
thg 7 25
562
Search a message Đã xử lý
1
thg 2 25
1262
0
thg 9 23
2224