[Odoo] Thêm / Update / Xóa dữ liệu field One2many, Many2many


Để thao tác CRUD dữ liệu cho các loại field One2manyMany2many trên Odoo thì Odoo định nghĩa các flags như sau:

Nguyên bản tiếng Anh

  • (0, 0, { values }) link to a new record that needs to be created with the given values dictionary
  • (1, ID, { values }) update the linked record with id = ID (write values on it)
  • (2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
  • (3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
  • (4, ID) link to existing record with id = ID (adds a relationship)
  • (5) unlink all (like using (3, ID) for all linked records)
  • (6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4, ID) for each ID in the list of IDs)

Giải thích tiếng Việt

  • (0, 0, { values }) sẽ tạo mới 1 record với dữ liệu là values kiểu dictionary và liên kết với bảng hiện tại
  • (1, ID, { values }) update 1 record đã liên kết có id = ID với dữ liệu update là values kiểu dictionary
  • (2, ID) Nó sẽ gọi hàng Unlink trên bảng liên kết và xóa cả liên kết lẫn đối tượng liên kết
  • (3, ID) Nó chỉ xóa liên kết chứ ko xóa đối tượng liên kết.
  • (4, ID) Liên kết tới 1 bản ghi có id = ID
  • (5) Xóa bỏ toàn bộ các record đã liên kết và liên kết giữa chúng vơi bảng hiện tại.
  • (6, 0, [IDs]) Thay thế toàn bộ danh sách IDs liên kết.

VD: Insert dữ liệu

@api.multi
def Create_One2many_method(self):
    search_var = self.search([('staff_age', '=', 0)])

    search_var.write({    
        'stud_ids': [(0, 0, {
            'reg_no': 4200,
            'stud_email': 'anbulove@gmail.com',
            'stud_phone': '9788987689',
        })]
    })

Ngoài ra với Odoo 15, 16 bạn có thê sử dụng thêm Class Command:

  • Command.create(vals) | (0, 0, { values }) link to a new record that needs to be created with the given values dictionary
  • Command.update(ID, vals) | (1, ID, { values }) update the linked record with id = ID (write *values* on it)
  • Command.delete(ID) | (2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
  • Command.unlink(ID) | (3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
  • Command.link(ID) | (4, ID) link to existing record with id = ID (adds a relationship)
  • Command.clear() | (5) unlink all (like using (3,ID) for all linked records)
  • Command.set([IDs]) | (6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

Nguồn: vinasupport.com

             
SHARE

Bài viết liên quan

mode_edit Bình luận của bạn

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

account_circle
web