作為一名圖書管理員,我希望能管理書籍的借閱、歸還和續借記錄,並自動更新書藉借出及歸還狀態,以實時追蹤每本書的流通情況。
邏輯如下
1.圖書管理員 若有新書進入,會在 library.book 新增一筆 書藉資料
2.學生借書前,需先建立學生資料
3.學生借書時,在 library.book 新增一個借出的按鈕,按下狀態改為借出,需填入借出學生,並留下一筆借出記錄,借出記錄可利用odoo的記錄方式
4.學生歸還時,在 library.book 新增一個歸還的按鈕,按下狀態改為未借,並留下一筆歸還記錄
5.調整時,維持暨有模組運作
根據您的需求,這裡是設計完整的 Odoo 模組,該模組允許圖書管理員管理書籍的借閱、歸還及續借記錄,並自動更新書籍的借出及歸還狀態。以下設計確保系統上下文保持一致,並利用 Odoo 的內建記錄功能來跟蹤書籍的流通情況。
__manifest__.py這是模組的元數據文件,用於描述模組及其依賴項。
{
    'name': 'Library Management',
    'version': '17.0',
    'depends': ['base', 'mail'],
    'data': [
        'views/library_book_views.xml',
        'views/library_student_views.xml',
        'views/library_book_category_views.xml',
        'views/library_management_menu.xml',
        'security/ir.model.access.csv',
    ],
    'installable': True,
    'application': True,
    'license': 'LGPL-3',
}
models/library_book.py書籍模型 library.book 管理書籍資料、借出與歸還按鈕,並透過 Odoo 內建的消息記錄功能追蹤每次的借閱行為。
from odoo import models, fields, api, exceptions
from datetime import date
class LibraryBook(models.Model):
    _name = 'library.book'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = '書籍資料'# 繼承 mail.thread 以啟用 message_post 功能
    name = fields.Char(string='書名', required=True)
    author = fields.Char(string='作者', required=True)
    isbn = fields.Char(string='ISBN', required=True)
    category_id = fields.Many2one('library.book.category', string='分類')
    is_borrowed = fields.Boolean(string='是否已借出', default=False, tracking=True)  # 開啟 tracking
    borrowed_by = fields.Many2one('library.student', string='借書學生', tracking=True)
    borrow_date = fields.Date(string='借出日期', readonly=True, tracking=True)
    return_date = fields.Date(string='歸還日期', readonly=True, tracking=True)
    def action_borrow(self):
        for record in self:
            if record.is_borrowed:
                raise exceptions.UserError('此書籍已借出,無法再次借出。')
            if not record.borrowed_by:
                raise exceptions.UserError('請選擇借書學生。')
            record.is_borrowed = True
            record.borrow_date = date.today()
            record.message_post(body=f'書籍 {record.name} 已於 {record.borrow_date} 借出給學生 {record.borrowed_by.name}。')
    def action_return(self):
        for record in self:
            if not record.is_borrowed:
                raise exceptions.UserError('此書籍尚未借出,無法歸還。')
            record.is_borrowed = False
            record.return_date = date.today()
            record.message_post(body=f'書籍 {record.name} 已於 {record.return_date} 歸還。')
models/library_student.py學生模型 library.student 管理學生資料,供借閱書籍時選擇。
from odoo import models, fields
class LibraryStudent(models.Model):
    _name = 'library.student'
    _description = '學生資料'
    name = fields.Char(string='姓名', required=True)
    student_number = fields.Char(string='學號', required=True, unique=True)
    class_name = fields.Char(string='班級')
    email = fields.Char(string='電子郵件')
    phone = fields.Char(string='電話')
models/library_book_category.py書籍分類模型 library.book.category 管理書籍的分類信息。
from odoo import models, fields
class LibraryBookCategory(models.Model):
    _name = 'library.book.category'
    _description = '書籍分類'
    name = fields.Char(string='分類名稱', required=True)
    description = fields.Text(string='描述')
views/library_book_views.xml書籍表單視圖,包含借出與歸還的按鈕,並顯示借書學生和借出、歸還狀態。
<odoo>
    <record id="view_library_book_form" model="ir.ui.view">
        <field name="name">library.book.form</field>
        <field name="model">library.book</field>
        <field name="arch" type="xml">
            <form>
                <header>
                    <button name="action_borrow" string="借出" type="object" class="btn-primary"
                            modifiers="{'invisible': [('is_borrowed', '=', True)]}"/>
                    <button name="action_return" string="歸還" type="object" class="btn-secondary"
                            modifiers="{'invisible': [('is_borrowed', '=', False)]}"/>
                </header>
                <sheet>
                    <group>
                        <field name="name" string="書名"/>
                        <field name="author" string="作者"/>
                        <field name="isbn" string="ISBN"/>
                        <field name="category_id" string="分類"/>
                    </group>
                    <group>
                        <field name="is_borrowed" string="是否已借出" readonly="1"/>
                        <field name="borrowed_by" string="借書學生" />
                        <field name="borrow_date" string="借出日期" readonly="1"/>
                        <field name="return_date" string="歸還日期" readonly="1"/>
                    </group>
                </sheet>
                <div class="oe_chatter">
                    <field name="message_follower_ids" widget="mail_followers"/>
                    <field name="activity_ids" widget="mail_activity"/>
                    <field name="message_ids" widget="mail_thread"/>
                </div>
            </form>
        </field>
    </record>
        <!-- 書籍管理動作 -->
    <record id="action_library_books" model="ir.actions.act_window">
        <field name="name">書籍管理</field>
        <field name="res_model">library.book</field>
        <field name="view_mode">tree,form</field>
    </record>
</odoo>
views/library_student_views.xml學生資料表單視圖,管理學生資料並供借書選擇使用。
<odoo>
    <record id="view_library_student_form" model="ir.ui.view">
        <field name="name">library.student.form</field>
        <field name="model">library.student</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="name" string="姓名"/>
                        <field name="student_number" string="學號"/>
                        <field name="class_name" string="班級"/>
                        <field name="email" string="電子郵件"/>
                        <field name="phone" string="電話"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>
    <record id="view_library_student_tree" model="ir.ui.view">
        <field name="name">library.student.tree</field>
        <field name="model">library.student</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name" string="姓名"/>
                <field name="student_number" string="學號"/>
                <field name="class_name" string="班級"/>
                <field name="email" string="電子郵件"/>
            </tree>
        </field>
    </record>
</odoo>
views/library_management_menu.xml)這裡定義了書籍和學生管理的選單。
<odoo>
    <!-- 主選單 -->
    <menuitem id="menu_library_root" name="圖書館管理" sequence="1"/>
    <!-- 書籍管理選單 -->
    <menuitem id="menu_library_books" name="書籍管理" parent="menu_library_root" sequence="2" action="action_library_books"/>
    <!-- 學生管理選單 -->
    <menuitem id="menu_library_students" name="學生管理" parent="menu_library_root" sequence="3" action="action_library_students"/>
    <!-- 書籍分類管理選單 -->
    <menuitem id="menu_library_book_category" name="書籍分類管理" parent="menu_library_root" sequence="4" action="action_library_book_category"/>
</odoo>
security/ir.model.access.csv)設置書籍、學生及分類的存取權限。
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_library_book,書籍管理,model_library_book,,1,1,1,1
access_library_student,學生管理,model_library_student,,1,1,1,1
access_library_book_category,書籍分類管理,model_library_book_category,,1,1,1,1
library.book 模型中新增書籍資料,並記錄書籍的借出和歸還狀態。.student` 模型中新增學生資料。
message 功能記錄借閱操作。歸還時,按下 "歸還" 按鈕,狀態變為未借,並記錄歸還日期。library.book.category 管理書籍的分類。這個設計確保系統運作正確且保持模組上下文一致。若有其他需求或進一步調整,請隨時告知!
odoo的程式結構性很強,在除錯上有天生的優勢,我們很容易可以查出哪些程式有問題
以這次產出的內容為例,有幾個問題
在ChatGPT讓他補足
library_management_menu.xml
缺少書籍分類的選單
而且 library_student_views 及library_book_views 的action 不見了