## 關系模型
來自模型的記錄可能與另一個模型的記錄有關。例如,一個銷售訂單記錄與包含客戶數據的客戶記錄有關,也與它的銷售訂單行記錄有關。
練習
創建會話模型
對于模塊公開學院,我們認為一個模型 *sessions*: 一個課程是一個課程的出現,在一個給定的時間,為一個給定的觀眾講授。
創建一個模型 *sessions*. 一個會話有一個名字,一個開始日期,一個時間和一個座位的數目。添加一個動作和一個菜單項來顯示它們。通過菜單項使新的模型可見。
1. 創建類 *Session* 在 `openacademy/models.py`.
2. 在會話對象中添加訪問 `openacademy/view/openacademy.xml`.
*openacademy/models.py*
~~~ python
name = fields.Char(string="Title", required=True)
description = fields.Text()
class Session(models.Model):
_name = 'openacademy.session'
name = fields.Char(required=True)
start_date = fields.Date()
duration = fields.Float(digits=(6, 2), help="Duration in days")
seats = fields.Integer(string="Number of seats")
~~~
*openacademy/views/openacademy.xml*
~~~ xml
<!-- Full id location:
action="openacademy.course_list_action"
It is not required when it is the same module -->
<!-- session form view -->
<record model="ir.ui.view" id="session_form_view">
<field name="name">session.form</field>
<field name="model">openacademy.session</field>
<field name="arch" type="xml">
<form string="Session Form">
<sheet>
<group>
<field name="name"/>
<field name="start_date"/>
<field name="duration"/>
<field name="seats"/>
</group>
</sheet>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="session_list_action">
<field name="name">Sessions</field>
<field name="res_model">openacademy.session</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="session_menu" name="Sessions"
parent="openacademy_menu"
action="session_list_action"/>
</data>
</openerp>
~~~
Note
`digits=(6, 2)` 指定浮點數的精度:6是數字的總數,而2是逗號之后的數字。請注意,它導致在逗號之前的數字位數最多為4