今天要來做借書的流程,我們會使用兩種方法
再來要確定所需的欄位
接下來就來看code
先到models/library_checkout_stage.py
from odoo import fields, models
class CheckoutStage(models.Model):
_name = "library.checkout.stage"
_description = "Checkout Stage"
_order = "sequence"
name = fields.Char()
sequence = fields.Integer(default=10)
fold = fields.Boolean()
active = fields.Boolean(default=True)
state = fields.Selection(
[("new","Requested"),
("open","Borrowed"),
("done","Returned"),
("cancel", "Canceled")],
default="new",
)
新建的檔案也要到models/init.py添加
from . import library_checkout_stage
設定規則讓使用者只能讀取,到security/ir.model.access.csv
checkout_stage_user,Checkout Stage User,model_library_checkout_stage,library_app.library_group_user,1,0,0,0
checkout_stage_manager,Checkout Stage Manager,model_library_checkout_stage,library_app.library_group_manager,1,1,1,1
接下來在上面選單設置路徑跟選單項目,
分別到library_app/views/library_menu.xml
<menuitem id="menu_library_configuration"
name="Configuration"
parent="menu_library"
/>
跟library_checkout/views/library_menu.xml
<record id="action_library_stage"
model="ir.actions.act_window">
<field name="name">Stages</field>
<field name="res_model">
library.checkout.stage</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_library_stage"
name="Stages"
action="action_library_stage"
parent=
"library_app.menu_library_configuration"
/>
點選Configuration就能找到剛剛新增的stage。
再來設置預設的內容,以便程式可以運作
<odoo noupdate="1">
<record id="stage_new" model=
"library.checkout.stage">
<field name="name">Draft</field>
<field name="sequence">10</field>
<field name="state">new</field>
</record>
<record id="stage_open" model=
"library.checkout.stage">
<field name="name">Borrowed</field>
<field name="sequence">20</field>
<field name="state">open</field>
</record>
<record id="stage_done" model=
"library.checkout.stage">
<field name="name">Completed</field>
<field name="sequence">90</field>
<field name="state">done</field>
</record>
<record id="stage_cancel" model=
"library.checkout.stage">
<field name="name">Canceled</field>
<field name="sequence">95</field>
<field name="state">cancel</field>
</record>
</odoo>
最後再到data添加
"data/library_checkout_stage.xml",
畫面如下:
這邊要讓借書流程更好,需要注意幾點:
首先要查詢第一個紀錄,回傳stage model第一筆記錄,是以sequence排序的,所以回傳最小的。
到library_checkout/models/library_checkout.py
@api.model
def _default_stage_id(self):
Stage = self.env["library.checkout.stage"]
return Stage.search([("state", "=", "new")],
limit=1)
如果要看到所有紀錄,這邊回傳所有的stages:
@api.model
def _group_expand_stage_id(self, stages, domain,order):
return stages.search([], order=order)