odoo V10開發文檔(第一章:模塊構造,模型,視圖,繼承)

odoo模塊構造

1.odoo模塊由manifest定義,每個模塊同時也是一個python包,通過init文件進行導入,可以通過odoo內置命令生成一個簡單的模塊

odoo-bin scaffold <module name> <where to put it>

2.數據模型

odoo的數據模型通過繼承一個Model的類定義,__name字段定義數據模型名稱,例:

from odoo import models
class MinimalModel(models.Model):
    _name = 'test.model'

3.數據字段

  • odoo數據字段通過定義model的屬性來實現
from odoo import models, fields
class LessMinimalModel(models.Model):
    _name = 'test.model2'
    name = fields.Char()
  • 字段也可以添加屬性
    name=field.Char(required=True)
  • 通用屬性:
    string (unicode, default: field's name) 定義用戶在界面看到的名字
    required (bool, default: False) 是否為必選字段
    help (unicode, default: '') 用于用戶界面提示
    index (bool, default: False) 數據庫索引
  • 保留字段
    id (Id) 數據表主鍵
    create_date (Datetime) 創建時間
    create_uid (Many2one) 創建人id
    write_date (Datetime) 最近修改時間
    write_uid (Many2one) 最近修改人id
  • 特殊字段
    odoo所有表默認有一個name字段

4.數據文件

模塊數據通過xml文件定義,每個<record>對應創建或更新一條數據表數據

<odoo>
    <data>
        <record model="{model name}" id="{record identifier}">
            <field name="{a field name}">{a value}</field>
        </record>
    </data>
</odoo>

model是模型名稱,id是模型ID,field name是字段名字,value是值
數據文件需要通過manifest文件的進行導入,定義在data里代表總是導入,定義在demo里表示只在演示模式下才導入

5.菜單和響應動作

菜單可以通過menuitem來定義

<record model="ir.actions.act_window" id="action_list_ideas">
    <field name="name">Ideas</field>
    <field name="res_model">idea.idea</field>
    <field name="view_mode">tree,form</field>
</record>
<menuitem id="menu_ideas" parent="menu_root" name="Ideas" sequence="10"
          action="action_list_ideas"/>

注意:action必須在菜單之前定義,xml文件是順序加載的

視圖

  • 基本視圖是以模型的ir.ui.view定義的,視圖類型需要用arch字段來指定
<record model="ir.ui.view" id="view_id">
    <field name="name">view.name</field>
    <field name="model">object_name</field>
    <field name="priority" eval="16"/>
    <field name="arch" type="xml">
        <!-- view content: <form>, <tree>, <graph>, ... -->
    </field>
</record>
  • 列表(以列表形式展示數據)
<tree string="Idea list">
   <field name="name"/>
   <field name="inventor_id"/>
</tree>
  • 表單(用于編輯和新增數據)
<form string="Idea form">
    <group colspan="4">
        <group colspan="2" col="2">
            <separator string="General stuff" colspan="2"/>
            <field name="name"/>
            <field name="inventor_id"/>
        </group>

        <group colspan="2" col="2">
            <separator string="Dates" colspan="2"/>
            <field name="active"/>
            <field name="invent_date" readonly="1"/>
        </group>

        <notebook colspan="4">
            <page string="Description">
                <field name="description" nolabel="1"/>
            </page>
        </notebook>

        <field name="state"/>
    </group>
</form>
  • 多tab表單
 <sheet>
    <group>
        <field name="name"/>
    </group>
    <notebook>
        <page string="Description">
            <field name="description"/>
        </page>
        <page string="About">
            This is an example of notebooks
        </page>
    </notebook>
</sheet>
  • 表單視圖也支持原始html
<form string="Idea Form">
    <header>
        <button string="Confirm" type="object" name="action_confirm"
                states="draft" class="oe_highlight" />
        <button string="Mark as done" type="object" name="action_done"
                states="confirmed" class="oe_highlight"/>
        <button string="Reset to draft" type="object" name="action_draft"
                states="confirmed,done" />
        <field name="state" widget="statusbar"/>
    </header>
    <sheet>
        <div class="oe_title">
            <label for="name" class="oe_edit_only" string="Idea Name" />
            <h1><field name="name" /></h1>
        </div>
        <separator string="General" colspan="2" />
        <group colspan="2" col="2">
            <field name="description" placeholder="Idea description..." />
        </group>
    </sheet>
</form>
  • 搜索
    通過search元素來決定搜索時使用的字段
<record model="ir.ui.view" id="course_search_view">
    <field name="name">course.search</field>
    <field name="model">openacademy.course</field>
    <field name="arch" type="xml">
        <search>
            <field name="name"/>
            <field name="description"/>
        </search>
    </field>
</record>

數據模型之間關聯

一個開放學院可以對應有多項開設的課程,多個會議,會議有名字、開始時間、持續時間等屬性

class Courses(models.Model):
    _name = 'academy.courses'

    name = fields.Char(string="Title", required=True)
    description = fields.Text()
class Session(models.Model):
    _name = 'academy.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")

view.xml

<!-- 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>
 <!-- top level menu: no parent -->
<menuitem id="main_openacademy_menu" name="Open Academy"/>
<!-- A first level in the left side menu is needed
     before using action= attribute -->
<menuitem id="openacademy_menu" name="Open Academy"
          parent="main_openacademy_menu"/>
<menuitem id="session_menu" name="Sessions"
          parent="openacademy_menu"
          action="session_list_action"/>

其中digits=(6, 2) 是浮點數的定義

數據模型之間通過字段關聯
1.多對一
Many2one(other_model, ondelete='set null')
使用:print foo.other_id.name
2.一對多
One2many(other_model, related_field)
一對多是虛擬的關系(一對多定義時必須保證有對應的多對一關系存在,且related_field要一致),使用的時候以集合的方式

for other in foo.other_ids:
    print other.name

3.多對多
Many2many(other_model),以集合的方式讀取數據

for other in foo.other_ids:
    print other.name

實例:

#models.py
class Courses(models.Model):
    _name = 'academy.courses'

    name = fields.Char(string="Title", required=True)
    description = fields.Text()

    responsible_id = fields.Many2one('res.users',
        ondelete='set null', string="Responsible", index=True)

class Session(models.Model):
    _name = 'academy.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")

    instructor_id = fields.Many2one('res.partner', string="Instructor")
    course_id = fields.Many2one('openacademy.course',
        ondelete='cascade', string="Course", required=True)
        
#view.xml
<!-- course表單視圖 -->
<record model="ir.ui.view" id="course_form_view">
    <field name="name">courses.form</field>
    <field name="model">openacademy.courses</field>
    <field name="arch" type="xml">
        <form string="Course Form">
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="responsible_id"/>
                </group>
                 <notebook>
                    <page string="Description">
                        <field name="description"/>
                    </page>
                    <page string="About">
                        This is an example of notebooks
                    </page>
                </notebook>
            </sheet>
        </form>
    </field>
</record>

<!-- 課程視圖 -->
<record model="ir.ui.view" id="course_search_view">
    <field name="name">courses.search</field>
    <field name="model">openacademy.courses</field>
    <field name="arch" type="xml">
        <search>
            <field name="name"/>
            <field name="description"/>
        </search>
    </field>
</record>

 <record model="ir.ui.view" id="course_tree_view">
    <field name="name">courses.tree</field>
    <field name="model">openacademy.courses</field>
    <field name="arch" type="xml">
        <tree string="Course Tree">
            <field name="name"/>
            <field name="responsible_id"/>
        </tree>
    </field>
</record>

<!-- session 表單視圖 -->
<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>
                  <group string="General">
                      <field name="course_id"/>
                      <field name="name"/>
                      <field name="instructor_id"/>
                  </group>
                  <group string="Schedule">
                      <field name="start_date"/>
                      <field name="duration"/>
                      <field name="seats"/>
                  </group>
              </group>
          </sheet>
      </form>
  </field>
</record>
<!-- session tree/list view -->
<record model="ir.ui.view" id="session_tree_view">
    <field name="name">session.tree</field>
    <field name="model">openacademy.session</field>
    <field name="arch" type="xml">
        <tree string="Session Tree">
            <field name="name"/>
            <field name="course_id"/>
        </tree>
    </field>
</record>

<!-- session 列表視圖,點擊二級菜單進入 -->
<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="main_openacademy_menu" name="Open Academy"/>
<!-- 一級菜單  -->
<menuitem id="openacademy_menu" name="Open Academy"
          parent="main_openacademy_menu"/>
<!-- 二級菜單 鏈接到 session_list record -->
<menuitem id="session_menu" name="Sessions"
          parent="openacademy_menu"
          action="session_list_action"/>

重啟odoo,搜索academy并安裝,進入academy試試效果

繼承

1.模型繼承

odoo提供兩種繼承機制:

  1. 在模塊內直接修改在其他模塊中定義的model如:添加字段、重寫字段定義、添加約束條件、添加函數方法、重寫已定義方法
  2. 使用代理機制,子模型可以訪問父模型的屬性,且可以自定義其他屬性

2.視圖繼承

odoo提供視圖繼續用于在原有視圖上進行擴展,通過inherit_id字段來關聯父級視圖,在arch元素里通過添加xpath元素來引入父級視圖內容,例:

<record id="idea_category_list2" model="ir.ui.view">
    <field name="name">id.category.list2</field>
    <field name="model">idea.category</field>
    <field name="inherit_id" ref="id_category_list"/>
    <field name="arch" type="xml">
        <!-- find field description and add the field
             idea_ids after it -->
        <xpath expr="http://field[@name='description']" position="after">
          <field name="idea_ids" string="Number of ideas"/>
        </xpath>
    </field>
</record>

expr屬性用于指定從父視圖中選擇使用的元素
position指定該元素的的使用

inside表示添加到匹配到的元素后
replace表示替換匹配到的元素
before表示添加到匹配的元素前,與其同級
after表示同級添加到匹配到的元素后
attributes用于改變匹配元素的attribute屬性

實例:
使用模型繼承給partner模型添加一個instructor字段,一個多對多的session-partner關系,使用視圖繼承在partner表單視圖中展示
1.創建models/partner.py并在init里導入
2.創建views/partner.xml并在manifest加載

#models/__init__.py
from . import models
from . import partner

#__manifest__.py
'data': [
    'security/ir.model.access.csv',
    'views/views.xml',
    'views/partner.xml',
    ]

#models/partner.py
# -*- coding: utf-8 -*-
from odoo import fields, models

class Partner(models.Model):
    _inherit = 'res.partner'

    # Add a new column to the res.partner model, by default partners are not
    # instructors
    instructor = fields.Boolean("Instructor", default=False)

    session_ids = fields.Many2many('openacademy.session',
        string="Attended Sessions", readonly=True)

#views/partner.xml
<?xml version="1.0" encoding="UTF-8"?>
 <odoo>
    <data>
        <!-- 在原來視圖基礎上添加instructor字段 -->
        <record model="ir.ui.view" id="partner_instructor_form_view">
            <field name="name">partner.instructor</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form"/>
            <field name="arch" type="xml">
                <notebook position="inside">
                    <page string="Sessions">
                        <group>
                            <field name="instructor"/>
                            <field name="session_ids"/>
                        </group>
                    </page>
                </notebook>
            </field>
        </record>

        <record model="ir.actions.act_window" id="contact_list_action">
            <field name="name">Contacts</field>
            <field name="res_model">res.partner</field>
            <field name="view_mode">tree,form</field>
        </record>
        <menuitem id="configuration_menu" name="Configuration"
                  parent="main_openacademy_menu"/>
        <menuitem id="contact_menu" name="Contacts"
                  parent="configuration_menu"
                  action="contact_list_action"/>
    </data>
</odoo>

需要先卸載再重裝模塊上面的例子才能正確運行。

3.domain表達式

domain表達式類似于三元表達式,對數據進行篩選
[('product_type', '=', 'service'), ('unit_price', '>', 1000)] 此表達式篩選出產品類型為service而且價格大于1000的記錄
& ,|, ! 羅輯運算符可用于多表達式聯接,但它是寫在domain表達式之前的而不是在兩個表達式之間

['|',
    ('product_type', '=', 'service'),
    '!', '&',
        ('unit_price', '>=', 1000),
        ('unit_price', '<', 2000)]

上面表達式篩選出產品類型為service或價格不在1000-2000之間的記錄
domain表達式可作用于關聯字段,用來控制顯示在客戶端的數據

#models.py
#在session模型中對instructor_id進行限制,只有instructor被設置為true的才顯示
 instructor_id = fields.Many2one('res.partner', string="Instructor",
        domain=[('instructor', '=', True)])
# instructor為true或者partner分類為teacher(teacher LV1,teacher LV2)
instructor_id = fields.Many2one('res.partner', string="Instructor",
        domain=['|', ('instructor', '=', True),
                     ('category_id.name', 'ilike', "Teacher")])
#partner.xml
 <record model="ir.actions.act_window" id="contact_cat_list_action">
    <field name="name">Contact Tags</field>
    <field name="res_model">res.partner.category</field>
    <field name="view_mode">tree,form</field>
</record>
<menuitem id="contact_cat_menu" name="Contact Tags"
          parent="configuration_menu"
          action="contact_cat_list_action"/>
<record model="res.partner.category" id="teacher1">
    <field name="name">Teacher / Level 1</field>
</record>
<record model="res.partner.category" id="teacher2">
    <field name="name">Teacher / Level 2</field>
</record>

重啟進入,instructor欄目只會顯示篩選過后的記錄了

數據模型自定義字段

1.實時計算字段
odoo模型不僅可以使用數據庫的字段,還可以通過函數實時計算 定義字段,在model里self是一個集合,可以使用循環來讀取,每一個循環得到單條記錄

import random
from odoo import models, fields, api

class ComputedModel(models.Model):
    _name = 'test.computed'

    name = fields.Char(compute='_compute_name')

    @api.multi
    def _compute_name(self):
        for record in self:
            record.name = str(random.randint(1, 1e6))

實時函數調用的字段往往依賴于其他字段,odoo提供depends裝飾器用于聲明,可以為ORM提供一個觸發器:@api.depends('seats', 'attendee_ids')

實例:為session模型添加一個上座率字段并用加載條顯示

#models.py 添加
    taken_seats = fields.Float(string="Taken seats", compute='_taken_seats')
    @api.depends('seats', 'attendee_ids')
    def _taken_seats(self):
        for r in self:
            if not r.seats:
                r.taken_seats = 0.0
            else:
                r.taken_seats = 100.0 * len(r.attendee_ids) / r.seats
#views.xml 
#在session的form和tree view里添加field元素
<field name="taken_seats" widget="progressbar"/>

2.默認值字段
定義字段時可使用default=x選項,x可以是特定的值或者通過計算得來

name = fields.Char(default="Unknown")
user_id = fields.Many2one('res.users', default=lambda self: self.env.user)

self.env提供了很多有用的特性

self.env.cr 或 self._cr -- 當前數據庫查詢游標
self.env.uid 或 self._uid -- 當前用戶在數據庫中id
self.env.user -- 當前用戶在數據庫中的記錄
self.env.context 或 self._context -- 當前上下文dictionary
self.env.ref(xml_id) -- 當前數據對應XML
self.env[model_name] -- 返回給定model的實例

實例:

start_date = fields.Date(default=fields.Date.today)
active = fields.Boolean(default=True)

onchange

odoo有一個onchange機制,不需要保存數據到數據庫就可以實時更新用戶界面上的顯示
例:


# models.py
# onchange handler
#假設有總量、單價、數量,在變更單價時總量實時更新
@api.onchange('amount', 'unit_price')
def _onchange_price(self):
    # set auto-changing field
    self.price = self.amount * self.unit_price
    # Can optionally return a warning and domains
    return {
        'warning': {
            'title': "Something bad happened",
            'message': "It was very bad indeed",
        }
    }
#監聽座位數的改變,實時校驗數值是否有誤,輸入錯誤數據時會彈框報錯
@api.onchange('seats', 'attendee_ids')
def _verify_valid_seats(self):
    if self.seats < 0:
        return {
            'warning': {
                'title': "Incorrect 'seats' value",
                'message': "The number of available seats may not be negative",
            },
        }
    if self.seats < len(self.attendee_ids):
        return {
            'warning': {
                'title': "Too many attendees",
                'message': "Increase seats or remove excess attendees",
            },
        }

模型約束

odoo提供兩種方法來校驗數據:python程序校驗和sql約束

  • python程序用constrains()方法來校驗數據,裝飾器方法定義需要校驗的字段,校驗函數在數據不合法時拋出一個錯誤
from odoo.exceptions import ValidationError
@api.constrains('age')
def _check_something(self):
    for record in self:
        if record.age > 20:
            raise ValidationError("Your record is too old: %s" % record.age)
    # all records passed the test, don't return anything
  • sql約束使用的是模型的_sql_constraints屬性,里面是一個校驗列表,每一個元素有三個子元素,(name, sql_definition, message),name是自定義的約束名稱、sql_definition是一個sql表達式、message是校驗失敗時返回的錯誤消息
_sql_constraints = [
       ('name_description_check',
        'CHECK(name != description)',
        "The title of the course should not be the description"),

       ('name_unique',
        'UNIQUE(name)',
        "The course title must be unique"),
   ]

內容發布自http://www.dingyii.cn,轉載請注明出處

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容