Author: |
My Company |
License: |
no license |
Branch: |
14.0 |
Repository: |
twtrubiks/odoo-demo-addons-tutorial |
Dependencies: |
hr_expense |
Languages: |
Markdown (107, 69.5%),
Python (23, 14.9%),
and
XML (24, 15.6%) |
Other branches: |
master |
<h1>odoo 繼承 - class inheritance</h1>
<p>建議觀看影片, 會更清楚:smile:</p>
<p><a href="https://youtu.be/zgb_0MJ3q9w">Youtube Tutorial - odoo 繼承 - class inheritance</a></p>
<p>建議在閱讀這篇文章之前, 請先確保了解看過以下的文章 (因為都有連貫的關係)</p>
<p><a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_odoo_tutorial">odoo 手把手建立第一個 addons</a></p>
<p>本篇文章主要介紹 class inheritance 這部份</p>
<h2>說明</h2>
<p><code>_inherit</code> class inheritance</p>
<p>注意, 還有一個是 <code>_inherits</code>, 不要搞錯了哦.</p>
<p>通常 <code>_inherit</code> 是去修改或是去擴充既有的 model,</p>
<p>使用情境可能如下,</p>
<p>像是在一個既有的 model 上增加一個 fields.</p>
<p>覆蓋掉一個已經存在的 model 中的 fields 定義.</p>
<p>增加 constraints 到一個既有的 model 上.</p>
<p>增加額外的 method 到一個既有的 model 上.</p>
<p>覆蓋掉一個已經存在的 model 中的 method.</p>
<p>這張圖是 odoo 中繼承的種類, 今天介紹 class inheritance,</p>
<p><img src="https://i.imgur.com/2aQ8BNh.png" alt="alt tag"></p>
<p>先來看 <a href="models/models.py">models/models.py</a></p>
<p>```python
......
class ClassInheritance(models.Model):
_name = 'hr.expense' # 可寫可不寫
_inherit = ['hr.expense']</p>
<pre><code>test_field = fields.Char('test_field')
</code></pre>
<p>......
```</p>
<p>目標是去繼承 <code>hr.expense</code>, 並且增加一個 <code>fields</code>.</p>
<p><code>_name = 'hr.expense' # 可寫可不寫</code></p>
<p><code>hr.expense</code> 是一個既有的 model, 所以在 <code>__manifest__.py</code> 中有 depends 關係</p>
<p>(記得一定要寫 depends, 不然會出現錯誤:exclamation:)</p>
<p><code>python
......
'depends': ['hr_expense'],
......
</code></p>
<p><code>_name</code> 和 <code>_inherit</code> 在這邊的名稱都是一樣的,</p>
<p>注意, 請不要自己定義一個 <code>_name</code> (和 <code>_inherit</code> 不一致), 因為這是另一個東西(如下圖, 以後說明).</p>
<p><img src="https://i.imgur.com/kjtCar6.png" alt="alt tag"></p>
<p><code>_inherit = ['hr.expense']</code></p>
<p>主要去繼承 <code>hr.expense</code>, 所以一定要有 depends:exclamation::exclamation:</p>
<p>當你安裝好 addons, 我們到資料庫中可以找到剛剛新增的 test_field</p>
<p><img src="https://i.imgur.com/MOFEDXy.png" alt="alt tag"></p>
<p>簡單說這種繼承的方式就是在繼承的 model 上增加新功能.</p>
<p><a href="views/views.xml">views/views.xml</a></p>
<p>```xml
......
<record id="view_expenses_tree_custom" model="ir.ui.view">
<field name="name">hr.expense.tree.custom</field>
<field name="model">hr.expense</field>
<field name="inherit_id" ref="hr_expense.view_expenses_tree"/>
<field name="arch" type="xml">
<field name="date" position="after">
<!-- <field name="test_field" groups="product.group_sale_pricelist" readonly="1"/> -->
<field name="test_field"/>
</field></p>
<pre><code> <!-- xpath the same result -->
<!--views/views.xml
<xpath expr="//field[@name='date']" position="after">
<field name="test_field" />
</xpath>
-->
</code></pre>
<p></field>
</record>
......
```</p>
<p><img src="https://i.imgur.com/PobVtjJ.png" alt="alt tag"></p>
<p>找 fields 的時候有兩種方式可以找,</p>
<p>第一種, 比較簡單的方法, 直接找到 fields, 然後定義 position 即可</p>
<p><code>xml
......
<field name="date" position="after">
<field name="test_field"/>
</field>
......
</code></p>
<p>第二種, 使用 xpath 的語法, 稍微比較複雜一點, 但是當你一個 view</p>
<p>裡面有重複的 fields 時, 就比較適合使用 xpath, 因為如果你使用第</p>
<p>一種方法, 會導致找不到 (有重複它會不知道要找哪一個:grimacing:)</p>
<p><code>xml
......
<xpath expr="//field[@name='date']" position="after">
<field name="test_field" />
</xpath>
......
</code></p>
<p>所以可以依照自己的需求下去選擇.</p>
<p><a href="views/views.xml">views/views.xml</a></p>
<p>form 的部份</p>
<p><code>xml
......
<record id="hr_expense_view_form_custom" model="ir.ui.view">
<field name="name">hr.expense.view.form.custom</field>
<field name="model">hr.expense</field>
<field name="inherit_id" ref="hr_expense.hr_expense_view_form"/>
<field name="arch" type="xml">
<field name="employee_id" position="after">
<field name="test_field"/>
</field>
</field>
</record>
......
</code></p>
<p><img src="https://i.imgur.com/DXJ4xK2.png" alt="alt tag"></p>
<h2>延伸閱讀</h2>
<ul>
<li><p><a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_prototype_inheritance">demo<em>prototype</em>inheritance</a></p></li>
<li><p><a href="https://github.com/twtrubiks/odoo-demo-addons-tutorial/tree/master/demo_delegation_inheritance">demo<em>delegation</em>inheritance</a></p></li>
</ul>