Day4 Let's ODOO: Model(1) Class & Attribute

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)

Model Class

以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 Attribute

列出几项常见设定的Model属性,在model内的_auto_register_abstract_transient只要根据需要的model型别去继承便不用另外设定。

  • _name: model名称,也会依照此名称建立资料表,要注意的是中间必须以. 分隔,如上述例子中的res.student,ODOO便会创立一个名为「res_student」的资料表,若name为空,则会在原本继承的资料表中扩增栏位。
  • _inherit: 继承model名称,如范例继承了res_partner model。
  • _inherits: 以特定栏位继承原本的表,会透过id参考回去原本的表。
  • _description: model叙述。
  • _table: 此model产生资料表的名称,通常不会额外设定,会由_name 自动产生对应的资料表。
  • _order: 填入fields name,再观察资料表的时候就会以此排序,如填入birthday 在ODOO观看时便会依照学生生日排序,预设是照id 排序。

属性与类别今天就介绍到这了,还有更多的参数可以参考ODOO官方文件,明天我们会继续介绍Model中的Fields。


<<:  从 JavaScript 角度学 Python(18) - 档案处理

>>:  Day 4 Odoo 资料库栏位建立

【第二二天 - Flutter GitHub Search 范例+RxDart+搜寻快取】

前言 今日的程序码 => GTIHBU 今天来讲讲搜寻的介绍。那当然,肯定要以 github ...

D20 - 用 Swift 和公开资讯,打造投资理财的 Apps { 移动平均线(MA线)实作.3 }

扩充 MAUtility,让原来的 func 能计算 n 条均线 在原来的 func 上加上 ran...

DAY29-VSCODE安装

直接在官网上下载即可。 这边选择自己的系统 这里将这两个打勾就能直接用右键开启 ...

Day 12. 生命周期 - Lifecycle Hooks

Instance Lifecycle 生命周期 介绍完怎麽建立Vue instance 後,接着来谈...

django新手村13-----路由规则

urls.py str有可以用int path('personal_info<str:name...