Đây là module mẫu quản lý thực tập sinh trên Odoo 19. Tài liệu này trình bày quy trình tạo module từ template my_module.zip, cách cài đặt vào Odoo trên Windows, cách kiểm tra chức năng, và cách chạy unit test.
Trong thư mục intern_demo/, các thành phần chính gồm:
__manifest__.py: Khai báo thông tin module và danh sách file dữ liệu (security, data, views, demo).__init__.py: Điểm khởi tạo module (importmodelsvàcontrollers).models/: Định nghĩa model (Python).views/: Định nghĩa giao diện và menu (XML).security/: Phân quyền truy cập (ACL).data/: Dữ liệu cấu hình (ví dụ: sequence).demo/: Dữ liệu mẫu (chỉ nạp khi database bật “Demo data”).tests/: Unit test theo cơ chế test của Odoo.controllers/: Mẫu controller (HTTP route). Nếu không sử dụng tính năng web route, phần này không ảnh hưởng đến chức năng quản lý nội bộ.
- Tải template
my_module.ziptheo tài liệu chính thức của Odoo: https://www.odoo.com/documentation/19.0/administration/odoo_sh/first_module.html#scaffolding-the-module - Giải nén. Kết quả sẽ có thư mục
my_module/.
- Đổi tên thư mục
my_module/thànhintern_demo/. - Kiểm tra trong
intern_demo/có các thành phần cơ bản:__manifest__.py,__init__.py,models/,views/,security/.
Mở intern_demo/__manifest__.py và kiểm tra tối thiểu:
name,summary,version,category.dependsđể['base']cho module demo đơn giản.dataphải có:security/ir.model.access.csvdata/intern_demo_sequence.xmlviews/intern_views.xml
demo(tùy chọn):demo/demo.xml.
Module này dùng 3 model kỹ thuật:
intern.demo: Thực tập sinh.intern.department: Phòng ban.intern.skill: Kỹ năng.
Các file tương ứng:
intern_demo/models/intern.py: modelintern.demo(tự sinh mã bằng sequence, tính số ngày thực tập, ràng buộc ngày kết thúc không được trước ngày bắt đầu).intern_demo/models/department.py: modelintern.department(liên kết danh sách thực tập sinh theo phòng ban).intern_demo/models/skill.py: modelintern.skill.intern_demo/models/__init__.py: import đủ các file model.
Mở intern_demo/security/ir.model.access.csv và đảm bảo có quyền truy cập cho:
intern.demointern.departmentintern.skill
Mở intern_demo/data/intern_demo_sequence.xml và kiểm tra khai báo sequence cho code intern.demo (ví dụ prefix INT-, padding 4).
Mở intern_demo/views/intern_views.xml và kiểm tra có:
- Views cho Intern: list, form, search.
- Views cho Department: list, form.
- Views cho Skill: list, form.
- Actions và menu để truy cập nhanh.
Lưu ý: Odoo 19 dùng thẻ <list> cho view danh sách, và action thường dùng view_mode là list,form.
File intern_demo/demo/demo.xml chứa dữ liệu mẫu để cài module kèm demo data.
Chép cả thư mục intern_demo vào:
C:\Program Files\Odoo 19.0.20260101\server\custom_addons\intern_demo
- Mở file cấu hình:
C:\Program Files\Odoo 19.0.20260101\server\odoo.conf - Tìm dòng
addons_path. - Đảm bảo có cả addons chuẩn và thư mục custom addons. Ví dụ:
addons_path = ...\server\odoo\addons, ...\server\custom_addons
Thực hiện 1 trong 2 cách sau.
Cách A:
- Mở Services:
services.msc. - Tìm service
odoo-server-19.0. - Chọn Restart.
Cách B (Command Prompt, Run as Administrator):
net stop odoo
net start odoo
- Mở Odoo:
http://localhost:8069. - Đăng nhập bằng tài khoản có quyền cài Apps.
- Vào Apps.
- Bấm “Update Apps List”.
- Tìm “Intern Demo”.
- Bấm “Install”.
- Vào menu “Intern Demo”.
- Mở “Departments” và tạo một phòng ban.
- Mở “Skills” và tạo một kỹ năng.
- Mở “Interns” và tạo một thực tập sinh.
- Kiểm tra mã tự sinh và số ngày thực tập được tính tự động.
Ví dụ lệnh (tùy theo đường dẫn odoo-bin của môi trường cài đặt):
./odoo-bin -d <db_name> -i intern_demo --test-enable --stop-after-init
Phần này chỉ dùng khi cần xóa sạch dấu vết module trong database để cài lại từ đầu (ví dụ: đổi tên kỹ thuật, đổi model, hoặc đã thử cài nhiều lần).
- Trong Odoo, module phải ở trạng thái “Uninstalled”.
- Nên dừng Odoo trước khi thao tác database:
net stop odoo
- Mở pgAdmin 4.
- Kết nối tới PostgreSQL.
- Chọn đúng database của Odoo.
- Mở “Query Tool”.
SELECT id, name, state, latest_version
FROM ir_module_module
WHERE name = 'intern_demo';Nếu state = 'uninstalled' thì có thể tiếp tục bước xóa.
DELETE FROM ir_module_module
WHERE name = 'intern_demo';DELETE FROM ir_model_data
WHERE module = 'intern_demo';Chỉ dùng khi module đã “Uninstalled”. Bước này giúp dọn các view không còn được tham chiếu bởi ir_model_data.
DELETE FROM ir_ui_view
WHERE id NOT IN (
SELECT res_id
FROM ir_model_data
WHERE model = 'ir.ui.view'
);- Khởi động lại Odoo:
net start odoo
- Vào Apps → “Update Apps List” → tìm lại module để cài đặt.
Nguyên nhân phổ biến là Odoo vẫn đọc source code module từ một đường dẫn khác trong addons_path.
- Mở
C:\Program Files\Odoo 19.0.20260101\server\odoo.conf. - Kiểm tra
addons_pathđể xác định chính xác các thư mục Odoo đang quét.