odoo Model字段的参数

odoo Model字段的参数

class Field(object):
    """ The field descriptor contains the field definition, and manages accesses
        and assignments of the corresponding field on records. The following
        attributes may be provided when instanciating a field:

        :param string: the label of the field seen by users (string); if not
            set, the ORM takes the field name in the class (capitalized).

        :param help: the tooltip of the field seen by users (string)

        :param readonly: whether the field is readonly (boolean, by default ``False``)

        :param required: whether the value of the field is required (boolean, by
            default ``False``)

        :param index: whether the field is indexed in database (boolean, by
            default ``False``)

        :param default: the default value for the field; this is either a static
            value, or a function taking a recordset and returning a value; use
            ``default=None`` to discard default values for the field

        :param states: a dictionary mapping state values to lists of UI attribute-value
            pairs; possible attributes are: 'readonly', 'required', 'invisible'.
            Note: Any state-based condition requires the ``state`` field value to be
            available on the client-side UI. This is typically done by including it in
            the relevant views, possibly made invisible if not relevant for the
            end-user.

        :param groups: comma-separated list of group xml ids (string); this
            restricts the field access to the users of the given groups only

        :param bool copy: whether the field value should be copied when the record
            is duplicated (default: ``True`` for normal fields, ``False`` for
            ``one2many`` and computed fields, including property fields and
            related fields)

        :param string oldname: the previous name of this field, so that ORM can rename
            it automatically at migration

        .. _field-computed:

        .. rubric:: Computed fields

        One can define a field whose value is computed instead of simply being
        read from the database. The attributes that are specific to computed
        fields are given below. To define such a field, simply provide a value
        for the attribute ``compute``.

        :param compute: name of a method that computes the field

        :param inverse: name of a method that inverses the field (optional)

        :param search: name of a method that implement search on the field (optional)

        :param store: whether the field is stored in database (boolean, by
            default ``False`` on computed fields)

        :param compute_sudo: whether the field should be recomputed as superuser
            to bypass access rights (boolean, by default ``False``)

        The methods given for ``compute``, ``inverse`` and ``search`` are model
        methods. Their signature is shown in the following example::

            upper = fields.Char(compute='_compute_upper',
                                inverse='_inverse_upper',
                                search='_search_upper')

            @api.depends('name')
            def _compute_upper(self):
                for rec in self:
                    rec.upper = rec.name.upper() if rec.name else False

            def _inverse_upper(self):
                for rec in self:
                    rec.name = rec.upper.lower() if rec.upper else False

            def _search_upper(self, operator, value):
                if operator == 'like':
                    operator = 'ilike'
                return [('name', operator, value)]

        The compute method has to assign the field on all records of the invoked
        recordset. The decorator :meth:`odoo.api.depends` must be applied on
        the compute method to specify the field dependencies; those dependencies
        are used to determine when to recompute the field; recomputation is
        automatic and guarantees cache/database consistency. Note that the same
        method can be used for several fields, you simply have to assign all the
        given fields in the method; the method will be invoked once for all
        those fields.

        By default, a computed field is not stored to the database, and is
        computed on-the-fly. Adding the attribute ``store=True`` will store the
        field's values in the database. The advantage of a stored field is that
        searching on that field is done by the database itself. The disadvantage
        is that it requires database updates when the field must be recomputed.

        The inverse method, as its name says, does the inverse of the compute
        method: the invoked records have a value for the field, and you must
        apply the necessary changes on the field dependencies such that the
        computation gives the expected value. Note that a computed field without
        an inverse method is readonly by default.

        The search method is invoked when processing domains before doing an
        actual search on the model. It must return a domain equivalent to the
        condition: ``field operator value``.

        .. _field-related:

        .. rubric:: Related fields

        The value of a related field is given by following a sequence of
        relational fields and reading a field on the reached model. The complete
        sequence of fields to traverse is specified by the attribute

        :param related: sequence of field names

        Some field attributes are automatically copied from the source field if
        they are not redefined: ``string``, ``help``, ``readonly``, ``required`` (only
        if all fields in the sequence are required), ``groups``, ``digits``, ``size``,
        ``translate``, ``sanitize``, ``selection``, ``comodel_name``, ``domain``,
        ``context``. All semantic-free attributes are copied from the source
        field.

        By default, the values of related fields are not stored to the database.
        Add the attribute ``store=True`` to make it stored, just like computed
        fields. Related fields are automatically recomputed when their
        dependencies are modified.

        .. _field-company-dependent:

        .. rubric:: Company-dependent fields

        Formerly known as 'property' fields, the value of those fields depends
        on the company. In other words, users that belong to different companies
        may see different values for the field on a given record.

        :param company_dependent: whether the field is company-dependent (boolean)

        .. _field-sparse:

        .. rubric:: Sparse fields

        Sparse fields have a very small probability of being not null. Therefore
        many such fields can be serialized compactly into a common location, the
        latter being a so-called "serialized" field.

        :param sparse: the name of the field where the value of this field must
            be stored.

        .. _field-incremental-definition:

        .. rubric:: Incremental definition

        A field is defined as class attribute on a model class. If the model
        is extended (see :class:`~odoo.models.Model`), one can also extend
        the field definition by redefining a field with the same name and same
        type on the subclass. In that case, the attributes of the field are
        taken from the parent class and overridden by the ones given in
        subclasses.

        For instance, the second class below only adds a tooltip on the field
        ``state``::

            class First(models.Model):
                _name = 'foo'
                state = fields.Selection([...], required=True)

            class Second(models.Model):
                _inherit = 'foo'
                state = fields.Selection(help="Blah blah blah")

    """

1.基础文件及目录结构

在认识odoo ORM框架前,先介绍一下odoo中模块目录结构。
?

?

data:存放模块预制数据
i18n:存放国际化文件
models:存放模型等py代码
security:存放权限文件
views:存放视图文件
__manifest__.py:该文件用于声明该模块,并指定一些模块元数据。(odoo8时该文件为__openerp__.py。)
?

# -*- coding: utf-8 -*-
{
    # name:模块名称
    'name': " test",
    # description:模块描述
    'description': """
        自定义模块
    """,
    # author:模块作者(XXX公司或张三)
    'author': "Hu",
    # website:作者或公司网址
    'website': "http://weibo.com/hcw1202",
    # category:模块分类
    'category': "test",
    # version:模块版本
    'version': "版本",
    # depends:所依赖其他模块
    'depends': ["base","stock","sale"],
    # 模块安装时加载
    'data': [
        'security/权限文件.csv',
        'data/预制数据.xml',
        'views/视图文件.xml',
    ],
    # 创建数据库时勾选Load demonstration data后安装该模块加载演示数据
    'demo': [
       'data/演示数据.xml',
],
}

2.Model属性

在/models下添加test.py文件

# -*- coding: utf-8 -*-
from odoo import models, api, fields, _
class Test(models.Model):
    # 模型唯一标识(对应数据表为product_manage_product)
    _name = 'product_manage.product'
    # 数据显示名称,如设置则返回其指定的字段值
    _rec_name = 'test_field'
    # 字段
    test_field = fields.Char(string="字段名称")

model属性详解:
_name:模型唯一标识,类非继承父类时必须指定。
_rec_name:数据显示名称,如设置则返回其指定的字段值,不设置默认显示字段为name的字段值,如无name字段则显示"模块名,id";详见BaseModel.name_get方法。
_log_access:是否自动增加日志字段(create_uid,?create_date,write_uid,?write_date)。默认为True。
_auto:是否创建数据库对象。默认为True,详见BaseModel._auto_init方法。
_table:数据库对象名称。缺省时数据库对象名称与_name指定值相同(.替换为下划线)。
_sequence:数据库id字段的序列。默认自动创建序列。
_order:数据显示排序。所指定值为模型字段,按指定字段和方式排序结果集。

例:_order = "create_date desc":根据创建时间降序排列。可指定多个字段。
不指定desc默认升序排列;不指定_order默认id升序排列。

_constraints:自定义约束条件。模型创建/编辑数据时触发,约束未通过弹出错误提示,拒绝创建/编辑。

格式:_constraints = [(method, ‘error message‘, [field1, ...]), ...]
method:检查方法。返回True|False
error message:不符合检查条件时(method返回False)弹出的错误信息
[field1, ...]:字段名列表,这些字段的值会出现在error message中。

_sql_constraints:数据库约束。

例:_sql_constraints = [ (‘number_uniq‘, ‘unique(number, code)‘, ‘error message‘) ]
会在数据库添加约束:
CONSTRAINT number_uniq UNIQUE(number, code)

_inherit:单一继承。值为所继承父类_name标识。如子类不定义_name属性,则在父类中增加该子类下的字段或方法,不创建新对象;如子类定义_name属性,则创建新对象,新对象拥有父类所有的字段或方法,父类不受影响。

格式:_inherit = ‘父类 _name‘

_inherits:多重继承。子类通过关联字段与父类关联,子类不拥有父类的字段或方法,但是可以直接操作父类的字段或方法。

格式:_inherits = {‘父类 _name‘: ‘关联字段‘}

3.字段属性

基础类型

Char:字符型,使用size参数定义字符串长度。
Text:文本型,无长度限制。
Boolean:布尔型(True,False)
Interger:整型
Float:浮点型,使用digits参数定义整数部分和小数部分位数。如digits=(10,6)
Datetime:日期时间型
Date:日期型
Binary:二进制型
selection:下拉框字段。

例:state = fields.Selection([(‘draft‘, ‘Draft‘),(‘confirm‘, ‘Confirmed‘),(‘cancel‘, ‘Cancelled‘)], string=‘Status‘)

Html:可设置字体格式、样式,可添加图片、链接等内容。效果如下:
?

截于odoo自带项目管理模块

关系类型

One2many:一对多关系。

定义:otm = fields.One2many("关联对象 _name", "关联字段",string="字段显示名",...)
例:analytic_line_ids = fields.One2many(‘account.analytic.line‘, ‘move_id‘, string=‘Analytic lines‘)"

Many2one

定义:mto = fields.Many2one("关联对象 _name", string="字段显示名",...)
可选参数:ondelete,可选值为‘cascade’和‘null’,缺省为null。表示one端删除时many端是否级联删除。

Many2many

定义:mtm = fields.Many2many("关联对象 _name", "关联表/中间表","关联字段1","关联字段2",string="字段显示名",...)
其中,关联字段、关联表/中间表可不填,中间表缺省为:表1_表2_rel
例:partner_id= fields.Many2many("res.partner", string="字段显示名",...)"

复杂类型

参数

readonly:是否只读,缺省值False。
required:是否必填,缺省值Falsse。
string:字段显示名,任意字符串。
default:字段默认值
domain:域条件,缺省值[]。在关系型字段中,domain用于过滤关联表中数据。
help:字段描述,鼠标滑过时提示。
store:是否存储于数据库。结合compute和related使用。

例:sale_order = fields.One2many("sale.order", "contract_id",string="销售订单", domain=[(‘state‘,‘=‘,‘sale‘)])

compute:字段值由函数计算,该字段可不储存于数据库。

例:amount = fields.Float(string="金额总计", compute=‘_compute_amount’,store=True)
_compute_amount为计算函数。

related:字段值引用关联表中某字段。

以下代码表示:company_id引用hr.payroll.advicecompany_id

advice_id = fields.Many2one('hr.payroll.advice', string='Bank Advice')
company_id = fields.Many2one('res.company', related='advice_id.company_id',
string='Company', store=True)

4.最后

以上即是Model的主要属性,下一节会介绍Model中常用的方法

原文地址:https://www.cnblogs.com/Kingfan1993/p/10784020.html

时间: 2024-10-12 15:25:21

odoo Model字段的参数的相关文章

python-django 模型model字段类型说明

V=models.CharField(max_length=None<, **options>) #varchar V=models.EmailField(<max_length=75, **options="">) #varchar V=models.URLField(<verify_exists=true, **options="" max_length="200,">) #varchar V=models

Django model 字段类型及选项解析

model field 类型 1.AutoField 一个自增的IntegerField,一般不直接使用,Django会自动给每张表添加一个自增的primary key. 2.BigIntegerField 64位整数, -9223372036854775808 到 9223372036854775807.默认的显示widget 是 TextInput. 3.BinaryField ( Django 1.6 版本新增 ) 存储二进制数据.不能使用 filter 函数获得 QuerySet 4.B

Django model字段类型清单

转载:<Django model字段类型清单> Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField:一个自动递增的整型字段,添加记录时它会自动增长.你通常不需要直接使用这个字段:如果你不指定主键的话,系统会自动添加一个主键字段到你的model.(参阅自动主键字段) BooleanField:布尔字段,管理工具里会自动将其描述为checkbox. CharField:字符串字段,单行输入,用于较短的字符串,

django ORM中models的字段以及参数

字段: AutoField(Field)         - int自增列,必须填入参数 primary_key=True     BigAutoField(AutoField)         - bigint自增列,必须填入参数 primary_key=True         注:当model中如果没有自增列,则自动会创建一个列名为id的列         from django.db import models         class UserInfo(models.Model):

Django中ORM介绍和字段及其参数

ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系数据库中. ORM在业务逻辑层和数据库层之间充当了桥梁的作用. ORM的由来 字母‘O’起源于“对象”(Object),'R'代表“关系”(Relational). 几乎所有的软件开发过程中都会涉及到对象和关系数据库.在用户层面和业务逻辑层

url分发、isinstance、request.GET请求之QueryDict和urlencode、post和get请求、limit_choices_to(Model字段)

这个的路径是怎么来的,是有一个个的url路由分发过来的 这两个是相等的,若url后面加括号了,那么前面就不用这个装饰器了:反之,若装饰器使用了,那么这个url后面就不要加括号了 eg:其他的views.test这是一个视图函数,而那个url()这个一个大列表,里面全是url,这里面就是所谓的二级分发的url, 到这里之后,这里都是函数的返回值,这是一个大的元祖形式,虽然没有括号,但是有逗号,这里既是一个大的元祖 再次点击get_url跳转到这里 这个u前面是一个正则,,分别是应用名,表明,后面m

django字段查询参数及聚合函数

字段查询是指如何指定SQL WHERE子句的内容.它们用作QuerySet的filter(), exclude()和get()方法的关键字参数. 默认查找类型为exact. 下表列出了所有的字段查询参数: 字段名 说明 exact 精确匹配 iexact 不区分大小写的精确匹配 contains 包含匹配 icontains 不区分大小写的包含匹配 in 在..之内的匹配 gt 大于 gte 大于等于 lt 小于 lte 小于等于 startswith 从开头匹配 istartswith 不区分

ORM字段和参数

常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. CharField varchar(xx) 字符类型,必须提供max_length参数, max_length表示字符长度. ForeignKey 外键,一般放在一对多'中'多'的一方 ManyToManyField 多对多关联,一般放在查询比较多的一方,列如查询作者的作品 DateField 日期字段,日期格式  YYYY-MM-DD,相当

Django&mdash;ORM常用字段和参数

一.ORM常用的字段和参数 1.0 常用字段 # AutoField: int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. # IntegerField: 一个整数类型,范围在 -2147483648 to 2147483647.(一般不用它来存手机号(位数也不够),直接用字符串存,) # CharField: 字符类型,必须提供max_length参数, max_length表示字符长度 # DateField: 日期字