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。
<<: 从 JavaScript 角度学 Python(18) - 档案处理
前言 今日的程序码 => GTIHBU 今天来讲讲搜寻的介绍。那当然,肯定要以 github ...
扩充 MAUtility,让原来的 func 能计算 n 条均线 在原来的 func 上加上 ran...
直接在官网上下载即可。 这边选择自己的系统 这里将这两个打勾就能直接用右键开启 ...
Instance Lifecycle 生命周期 介绍完怎麽建立Vue instance 後,接着来谈...
urls.py str有可以用int path('personal_info<str:name...