ODOO透過Model來定義資料表中的欄位與關聯,我們今天介紹一個Model的類別以及屬性。
我們以創立一個Student模組為例
在models/init.py import res.student
from . import res.student
並在models下建立 res_student.py
# -*- coding: utf-8 -*-
from odoo import api, models, fields
class ResStudent(models.Model):
_name = 'res.student'
_inherit = 'res.partner'
_description = 'Student'
nickname = fields.Char(string='綽號')
score = fields.Float(string='學期平均')
birthday = fields.Date(string='生日', required=True)
school = fields.Many2one('res.company', string='所屬學校')
gender = fields.Selection([("male", "Male"), ("female", "Female"), ("other", "Other")], string='性別')
is_leadership = fields.Boolean(default=False)
以BaseModel為基礎分了三個類別的Model:
Model :最常用的型別,在安裝addon時會對應Model產生表和欄位,一般來說沒有特殊需求就會宣告成這個型別。
class Model(AbstractModel):
""" Main super-class for regular database-persisted Odoo models.
Odoo models are created by inheriting from this class::
class user(Model):
...
The system will later instantiate the class once per database (on
which the class' module is installed).
"""
_auto = True # automatically create database backend
_register = False # not visible in ORM registry, meant to be python-inherited only
_abstract = False # not abstract
_transient = False # not transient
TransientModel: 產生的資料作為臨時資料使用,一段時間以後ODOO變會清除裡面的資料,一般用來建立wizard。
class TransientModel(Model):
""" Model super-class for transient records, meant to be temporarily
persistent, and regularly vacuum-cleaned.
A TransientModel has a simplified access rights management, all users can
create new records, and may only access the records they created. The
superuser has unrestricted access to all TransientModel records.
"""
_auto = True # automatically create database backend
_register = False # not visible in ORM registry, meant to be python-inherited only
_abstract = False # not abstract
_transient = True # transient
AbstractModel: 即是最原本的BaseModel,不會新增欄位及儲存資料,透過繼承其他model來新增功能。
AbstractModel = BaseModel
列出幾項常見設定的Model屬性,在model內的_auto
、_register
、_abstract
、_transient
只要根據需要的model型別去繼承便不用另外設定。
.
分隔,如上述例子中的res.student
,ODOO便會創立一個名為「res_student」的資料表,若name為空,則會在原本繼承的資料表中擴增欄位。res_partner
model。_name
自動產生對應的資料表。birthday
在ODOO觀看時便會依照學生生日排序,預設是照id
排序。屬性與類別今天就介紹到這了,還有更多的參數可以參考ODOO官方文件,明天我們會繼續介紹Model中的Fields。