简体   繁体   English

Odoo V15 - 如何使用 Onchange 函数设置一行

[英]Odoo V15 - How to set a line with an Onchange Function

I have this calendar that is made with Javascript.我有这个用 Javascript 制作的日历。

在此处输入图像描述

When you click in "Libre", a view opens, where you can create a reserve.当您单击“Libre”时,会打开一个视图,您可以在其中创建储备。 I want the reservation line as default and look something like this.我希望保留行作为默认行,看起来像这样。 In this case I did it manually, and I want it to appear automatically.在这种情况下,我是手动完成的,我希望它自动出现。 I know I need an on @api.onchange function.我知道我需要一个 on @api.onchange函数。

在此处输入图像描述

The roomid field ("Room" in the image) has the ID of the room that I need to add in reservation.line. roomid字段(图中的“Room”)包含我需要在 reservation.line 中添加的房间 ID。 I get this from.js我从.js得到这个

I have something like this in my code and it's getting exactly the room I want, but I don't know how to make it appear automatically.我的代码中有这样的东西,它正好得到我想要的房间,但我不知道如何让它自动出现。

class HotelReservation(models.Model):
    _name = "hotel.reservation"
   
  room_id = fields.Many2one("hotel.room", string="Room", required=True)

  roomid= fields.Integer(related='room_id.id', string="Room")

  reservation_line = fields.One2many(
        "hotel.reservation.line",
        "line_id",
        string="Reservation Line",
        help="Hotel room reservation details.",
        readonly=True,
        states={"draft": [("readonly", False)]},
    )


   @api.onchange('roomid')
   def onchange_reservation_line(self):
        if self.roomid:
            room = self.env['hotel.room'].search([('id' ,'=', self.roomid)])
            # Some return here?

You can read in the write function documentation that the expected value of a One2many or Many2many relational field is a list of Command that manipulates the relation the implement.您可以在write函数文档中读到, One2manyMany2many关系字段的预期值是一个命令列表,该列表操作该工具的关系。 There are a total of 7 commands: create() , update() , delete() , unlink() , link() , clear() , and set() .共有 7 个命令: create()update()delete()unlink()link()clear()set()

roomid is related to room_id so you can use it in onchange function and avoid calling the search method to get the room record. roomidroom_id相关,可以在onchange 函数中使用,避免调用search 方法获取房间记录。

Example:例子:

@api.onchange('room_id')
def onchange_reservation_line(self):
    if self.room_id:
        self.reservation_line = [
            Command.clear(),
            Command.create({
                'field_name': field_value,
                ...
            }),
        ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM