Câu hỏi:

tôi muốn tạo một model để Tạo màn hình import từ file excel vào table tạm trong odoo


Để tạo một model để import từ file Excel vào bảng tạm trong Odoo, bạn có thể làm theo các bước sau:

  1. Đầu tiên, bạn cần cài đặt và kích hoạt thư viện xlrd (để đọc file Excel) bằng cách sử dụng pip (Python package manager):
pip install xlrd
  1. Tạo một module mới trong Odoo. Để làm điều này, bạn cần tạo một thư mục mới trong thư mục addons của Odoo và thêm các tệp cần thiết như __init__.py, __manifest__.py, models.py, views.xml. Trong __manifest__.py, đừng quên thêm phụ thuộc vào thư viện xlrd.

  2. Trong tệp models.py, tạo một model mới kế thừa từ TransientModel của Odoo để định nghĩa bảng tạm. Ví dụ:

from odoo import models, fields

class ImportExcelWizard(models.TransientModel):
_name = 'import.excel.wizard'
_description = 'Import Excel Wizard'

file = fields.Binary('File')
file_name = fields.Char('File Name')
  1. Trong tệp views.xml, tạo một view để hiển thị màn hình import:
<odoo>
<record id="view_import_excel_wizard" model="ir.ui.view">
<field name="name">import.excel.wizard.form</field>
<field name="model">import.excel.wizard</field>
<field name="arch" type="xml">
<form string="Import Excel">
<group>
<field name="file" />
<field name="file_name" />
</group>
<footer>
<button name="import_data" string="Import" type="object" class="btn-primary" />
<button string="Cancel" class="btn-secondary" special="cancel" />
</footer>
</form>
</field>
</record>

<record id="action_import_excel_wizard" model="ir.actions.act_window">
<field name="name">Import Excel</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">import.excel.wizard</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_import_excel_wizard" />
<field name="target">new</field>
</record>
</odoo>
  1. Trong model đã tạo ở bước 3, thêm một phương thức để xử lý việc import dữ liệu từ file Excel:
import base64
import xlrd
from odoo import models, fields, api
from odoo.exceptions import UserError

class ImportExcelWizard(models.TransientModel):
_name = 'import.excel.wizard'
_description = 'Import Excel Wizard'

file = fields.Binary('File')
file_name = fields.Char('File Name')

@api.multi
def import_data(self):
if not self.file:
raise UserError("Vui lòng chọn tệp để import.")
xls_data = base64.b64decode(self.file)
workbook = xlrd.open_workbook(file_contents=xls_data)

# Truy cập sheet đầu tiên của file
sheet = workbook.sheet_by_index(0)

# Duyệt qua từng dòng và xử lý import dữ liệu
for row in range(1, sheet.nrows):
# Ví dụ: lấy dữ liệu từ các ô trong dòng hiện tại
col_0_value = sheet.cell_value(row, 0)
col_1_value = sheet.cell_value(row, 1)

# Lưu dữ liệu vào Odoo model (điều này phụ thuộc vào model bạn muốn import)
self.env['your.target.model'].create({
'column_1': col_0_value,
'column_2': col_1_value,
})

return {'type': 'ir.actions.act_window_close'}
  1. Cập nhật module của bạn trong Odoo và kiểm tra màn hình import đã được tạo. Bạn có thể gọi action action_import_excel_wizard từ menu, nút, hay code để mở màn hình import.

Lưu ý: Đoạn mã trên chỉ là ví dụ cơ bản để bạn tham khảo, trong thực tế bạn cần điều chỉnh theo yêu cầu dự án của mình.


Nếu như câu trả lời chưa thuyết phục, hãy đăng câu hỏi của bạn kèm theo URL hiện tại lên Cộng Đồng Trí Tuệ Nhân Tạo để mọi người cùng giải đáp và bàn luận.
Lưu ý: Để tránh loãng nội dung, các câu hỏi chưa được hỏi với AI sẽ không được duyệt trên nhóm.
Trang chủ | Hỏi tiếp | Xem lịch sử