字段和小组件

Fields structure the models of a database. If you think of a model as a table or spreadsheet, fields are the columns and records are the individual rows. Fields also define the type of data that is stored within them. The way in which a field’s data is presented and formatted on the UI is controlled by its widget.

With Studio, it is possible to:

Field types

Fields can be broadly divided into two categories:

  • Simple fields, which contain basic values, such as text, numbers, files, etc.

  • Relational fields, which are used to link and display the data from records on another model.

简单字段

简单字段包含基本值,如文本、数字、文件等。

注解

若有非预设的小工具可用,会在下方以点列符号或子标题的形式交代。

文件(char

:guilable:`文本`字段用于包含任何字符的短文本。填写该字段时显示一行文本。

  • :guilable:`标记`:显示圈内值,类似于标记。该值不能在用户界面上编辑,但可以设置默认值。

  • 复制到剪贴板:用户可以点击按钮复制值。

  • 电子邮件:该值变为可点击的*电子邮件*链接。

  • 图片:使用URL显示图片。该值不能手动编辑,但可以设置默认值。

    注解

    此方式与直接选择 图片字段 不同。若使用附有 图片 小工具的 文字 字段,图片不会储存在 Odoo 中。举例,如果想节省储存空间,这方式会很有用。

  • 电话:该值变为可点击的*电话*链接。

    小技巧

    勾选:guilabel:启用短信,添加直接从字段旁的Odoo发送短信的选项。

  • URL:该值变为可点击的URL。

Example

具有不同小组件的文本字段示例

多行文本(text

:guilable:`多行文本`字段用于包含任何类型字符的较长文本。填写该字段时,用户界面上显示两行文本。

  • 复制到剪贴板:用户可以点击按钮复制值。

Example

具有不同小组件的多行文本字段示例

整数(integer

:guilable:`整数`字段用于所有整数(:dfn:`positive, negative, or zero, without a decimal`)。

  • :guilable:`百分比饼图`:显示百分比饼图值,通常为计算得出的值。该值无法在用户界面上编辑,但可以设置默认值。

  • 进度条:显示百分比条旁的值,通常为计算得出的值。该字段无法手动编辑,但可以设置默认值。

  • 处理:显示拖动手柄图标,用于在:ref:`List view <studio/views/multiple-records/list>`中手动排序记录。

Example

具有不同小组件的整数字段示例

小数(float

小数`字段用于所有小数(:dfn:`positive, negative, or zero, with a decimal)。

注解

小数在用户界面上显示为两位小数,保存在数据库中时精度更高。

  • 货币金额:与使用 货币金额 字段类似。建议使用后者,因为它提供较多功能。

  • 百分比:在数值后显示百分比符号。

  • :guilable:`百分比饼图`:显示百分比饼图值,通常为计算得出的值。该字段无法手动编辑,但可以设置默认值。

  • 进度条:显示百分比条旁的值,通常为计算得出的值。该字段无法手动编辑,但可以设置默认值。

  • 时间:数值须为*小时:分钟*格式,最大分钟值为59。

Example

具有不同小组部件的小数字段示例

货币(monetary

:guilabel:`货币`字段用于所有货币值。

注解

首次添加:guilable:`货币`字段时,如果模型上不存在:guilabel:`币种`字段,则会提示添加:gullable:`币种`字段。Odoo为您添加:guilable:`币种`字段。添加完成后,应再次添加:guilable:`货币`字段。

Example

货币字段及其币种字段示例

超文本标记语言(html

:guilabel:`超文本标记语言`字段用于添加可使用Odoo HTML编辑器编辑的文本。

  • 多行文本:禁用Odoo HTML编辑器,以便允许编辑原超文本标记语言。

Example

具有不同小组件的超文本标记语言字段示例

日期(date

:guilabel:`日期`字段用于从日历选择日期。

  • 剩余天数:从目前日期起计,距离选取的日期还剩下多少天,例如: 剩余 5 天 。此栏位应设为 仅读

Example

具有不同小组件的日期字段示例

日期和时间(datetime

:guilabel:`日期和时间`字段用于从日历选择日期,从钟表选择时间。如果未设置时间,则自动采用用户的当前时间。

小技巧

一般属性 外,设置了 日期时间日期范围 小工具的 日期时间 栏位,有一些额外的 特定属性 可用。

日期范围(daterange

日期范围 小工具适用于显示包括开始日期及结束日期的时间段,以一行显示全部资料。日期范围可将开始及结束日期设为必填(例如:表示多天活动的日期),也可将开始或结束日期设为可选填(例如:用于上门介入工作或项目任务)。

加入日期范围时,需要有两个字段:一个是附有 日期范围 小工具的 日期时间 字段,另一个是设为开始日期 结束日期的字段。底层的参考字段可以是现有的 日期 或 :guilabel:`日期时间`字段,也可以是专门为此用途而建立的字段。

要加入日期范围:

  1. 确定一个现有的 日期日期时间 字段,用作底层的开始或结束日期参考字段,或新增一个字段。如果日期范围是:

    • 必须填写开始及结束日期,此字段可以是开始日期或结束日期,选择两者的结果相同。

    • 允许选填开始日期或结束日期,此字段便会分别是开始日期或结束日期。

    小技巧

    为避免重复显示相同资讯,你可将底层的开始/结束日期字段,设定为 不显示,或点击 从视图中移除,将该日期从视图画面中移除,使之无法看见。

  2. 加入一个 日期时间 字段,并将 小工具 栏位设定为 日期范围

  3. 输入适当的 标签

  4. 根据需要,在 开始日期字段结束日期字段 下拉式选单中,选择相应的底层开始/结束日期字段。

  5. 若日期范围必须包括开始日期及结束日期,请启用 总是使用范围

  6. 更新任何其他需要更新的 一般属性日期时间栏位特定属性,然后按一下画面右上角的 关闭

Example

具有不同小组件的日期和时间字段示例
剩余天数(remaining_days

剩余天数 小工具显示从目前的日期及时间起计,距离选取的日期还剩下多少天,例如:剩余 5 天 。此栏位应设为 仅读

复选框(boolean

:guilabel:`复选框`字段用于值只能为是或否时,选中或取消选中复选框,以进行指示。

  • 按钮:显示为单选按钮。该小组件可在不切换至编辑模式的情况下工作。

  • 切换:显示为切换按钮。该小组件可在不切换至编辑模式的情况下工作。

Example

具有不同小组件的复选框字段示例

选择(selection

:guilabel:`选择`字段用于用户从一组预定义值中选择一个值。

  • :guilable:`标记`:显示圈内值,类似于标记。该值不能在用户界面上编辑,但可以设置默认值。

  • 多个标记:在水平排列的矩形内同时显示所有可选值。

  • 优先级别:显示为星星图示,而非数值,可用作表示重要程度或满意度等。与选用 优先等级 字段的效果相同,但后者已预先定义了 4 种优先级别数值。

  • 单选: 同时显示所有可选值的单选按钮。

    小技巧

    默认情况下,单选按钮是以垂直方式排列。您可启用 水平显示 选项,以切换显示方式。

  • 状态栏:以箭头进度条同时显示所有可选值。

    小技巧

    默认情况下,状态栏上的值是可选取。禁用 可点击,可防止在用户界面上编辑值。

Example

具有不同小组件的选择字段示例

优先级(selection

优先级别 字段以三星评分格式显示,可用作表示重要程度或满意度。此字段类型是 选择字段,默认会选取 优先级别 小工具,并有 4 个预先定义的优先级别值。因此, 徽章单选按钮选单 小工具的效果,与 选单 章节所述相同。

小技巧

要通过添加或删除值来更改可用星的数量,点击:guilable:编辑值。请注意,第一个值等于0星(即未作选择),因此,三星评级系统具有4个值。

Example

优先级字段示例

文件(binary

:guilabel:`文件`字段用于上传任何类型的文件,或 签署表单(:guilabel:`签署`小组件)。

  • 图片:用户可以上传图像文件,然后将其显示在 表单视图 中。这与使用 :ref:`图片 <studio/fields/simple-fields-image>`字段的效果相同。

  • PDF查看器:用户可以上传PDF文件,上传后,可以在:ref:`Form view <studio/views/general/form>`中查看该文件。

  • 电子签名:用户可通过电子形式签署表单。与选择 电子签名 字段的效果相同。

Example

具有不同小组件的文件字段示例

图片(binary

图片`字段用于上传图片并在 :ref:`表单视图 <studio/views/general/form>`中显示。该字段类型是 :ref:`文件 <studio/fields/simple-fields-file>`字段,默认情况下选择了 :guilabel:`图片 小工具。因此,文件PDF 阅读工具电子签名 小工具的效果,与 文件 中描述的效果相同。

小技巧

要更改上传图片的显示大小,在:guilable:尺寸`选项下选择:guilabel:`小:guilale:`中`或:guilalb:`大`

签署(binary

电子签名 字段用于对表单进行电子签名。该字段类型是 文件字段,默认选择 电子签名`小工具。因此,:guilabel:`文件图片PDF 阅读工具 小工具有与 文件 中描述的相同效果。

小技巧

要让用户在绘制签名时使用 自动完成 选项,请从可用的 自动完成依据 字段(仅在模型上的 文字Many2One相关字段)中选择一个。签名将使用所选字段的数据自动生成。

关系字段

关系字段用于链接并显示另一模型中记录的数据。

注解

非默认小组件(如可用)显示为以下项目符号。

Many2One(many2one

:guilabel:`Many2One`字段用于将另一记录(来自另一模型)链接至正在编辑的记录。来自另一模型的记录名称将显示在正在编辑的记录中。

Example

在*销售订单*模型中,:guilable:`客户`字段为指向*联系人*模型的:guilabel:`Many2One`字段,允许**多个**销售订单链接至**一个**联系人(客户)。

显示many2one关系的图表

小技巧

  • 要防止用户在链接的模型中创建新记录,勾选:guilable:禁用创建

  • 要防止用户在弹窗中打开记录,勾选:guilable:禁用打开

  • 要帮助用户只选择正确的记录,点击:guilable:,创建筛选器。

  • 标记:显示圈内值,类似于标记。该值不能在用户界面上编辑。

  • 单选: 同时显示所有可选值的单选按钮。

One2Many(one2many

:guilabel:`One2Many`字段用于显示当前模型中的记录和另一模型中的多个记录间的现有关系。

Example

您可以在*联系人*模型中添加:guilable:`One2Many`字段,以查看**一个**客户的**多个**销售订单。

显示one2many关系的图表

注解

要使用 一对多 字段,两个模型必须已经用 多对一字段 连接。一对多关系不会独立存在,系统会进行反向搜寻,寻找目前已有的多对一关系。

多行(one2many

:guilabel:`多行`字段用于创建有多行和多列的表格(例如,销售订单中的多个产品行)。

小技巧

要修改列,点击:guilable:多行`字段,然后点击:gullable:`编辑列表视图。要编辑用户点击:guilable:Add a line`时弹出的表单,改为点击:gullable:`编辑表单视图

Example

多行字段示例

Many2Many(many2many

多对多 (Many2Many)字段用于将另一模型中的多项记录,连结至目前模型中的多项记录。与 多对一 字段一样,多对多字段也可使用 禁止建立禁止开启范围 功能。

Example

在*任务*模型中,:guilable:`受托人`字段为指向*联系人*模型的:guilabel:`Many2Many`字段,允许向单个用户分配**多个**任务,以及向**多个**用户分配给同一任务。

显示many2many关系的图表
  • 多个复选框:用户可以通过多个复选框选择多个值。

  • 标签:用户可选取多个以圆角形状显示的值,这些值也称为“标签”。这与选用 标签 字段的效果相同。

标签(many2many

标签`字段用于显示来自另一个模型的多个值,这些值以圆形显示,也称为**标签**。此字段类型是 :ref:`多对多字段 <studio/fields/relational-fields-many2many>,默认选择 标签 小工具。因此,复选框`和 :guilabel:`多对多 小工具有与 多对多 中描述的相同效果。

小技巧

要显示具有不同背景颜色的标记,勾选:guilable:使用颜色

Example

标记字段示例

Add a field to a view

With Studio, it is possible to add:

小技巧

Add a new field to a form or list view to save it to the model and make it available as an existing field in other views of the same model.

Add a new field

小技巧

Activate developer mode before adding a new field to see, and be able to edit, the field’s technical name during configuration.

To add a new field, follow these steps:

  1. Navigate to the relevant form or list view, then click the (Toggle Studio) icon in the upper-right corner. Alternatively, with any app open, click the (Toggle Studio) icon, then navigate to the relevant form or list view.

  2. In the Add tab, drag the relevant field type from the New Fields section and drop it in the desired position. If the new field is a relational field, select the relevant reciprocal field, model, or related field in the pop-up.

  3. In the Properties tab, configure the field’s properties:

    • Enter the Label to be displayed as the field name on the UI; this is also used to generate the default technical name of the field.

    • Click anywhere outside the Label field to see the Technical Name updated with the default technical name.

      注解

      • The Technical Name of a new field can be edited in Studio during the configuration process. At any other time, a field’s technical name can only be modified via the technical settings of the database.

      • The technical name of a new field added using Studio is by default prefixed by x_studio_.

    • Optionally, select a different Widget via the dropdown; the default widget for the field type is selected by default.

    • Optionally, configure any other field properties.

  4. Click Close in the top-right corner to close Studio.

Add an existing field

To add a field that exists on the model but that is not present on the current view, follow these steps:

  1. Navigate to the relevant view, then click the (Toggle Studio) icon in the upper-right corner. Alternatively, with any app open, click the (Toggle Studio) icon, then navigate to the relevant view.

  2. In the Add tab, in the left panel, click Existing Fields to reveal a list of all the model’s fields that are not currently present in the view.

  3. Drag the relevant field and drop it in the desired position.

  4. In the Properties tab, configure the relevant properties for the field in the current view.

    注解

    Various properties can be configured independently for different views, if needed, such as the Label, the Widget, the visibility of the field for certain user groups, as well as whether or not the field is Invisible, Required, or Readonly.

  5. Click Close in the top-right corner to close Studio.

Field properties

A series of general properties can be configured for most combinations of field type and widget. Additional properties are available depending on the specific field type and widget selected. For example, Date & Time fields using the Date & Time or Date Range widget have various specific properties that can be configured.

一般属性

  • 隐藏:若用户无需在操作介面看到该字段,便可启用此属性。这有助简化用户操作介面,只会显示个别情况需要显示的字段。

  • 必填:若用户必须先完成填写该字段才可继续操作,可启用此属性。

  • 仅读:如果不应让用户修改该字段,可以启用此属性。

小技巧

  • The Invisible property also applies inside Studio. To view hidden fields in Studio, click on a view’s View tab and enable Show Invisible Elements.

  • The Invisible, Required, and Readonly properties can be enabled for specific records only by clicking on Conditional and creating a filter.

    Example

    On the form view of the Contact model, the Title field only appears when Individual is selected, as that field would not be helpful for a Company contact.

  • Label: This is the field name on the UI; the label is used to generate the technical name of the field.

  • 帮助工具提示:要解释字段的用途,请添加说明。将鼠标悬停在字段标签旁的问号上时,文本将显示在工具提示框内。

  • Technical Name: To view a field’s technical name in Studio (and, during the configuration of a new field, edit it if desired), activate developer mode.

    小技巧

    If this field needs to be updated but is no longer editable via Studio, the technical name can be modified via the technical settings of the database.

  • 小组件:要更改某字段的默认外观或功能,选择可用小组件中的一个。

  • 占位符:为举例说明字段的填写方式,可添加占位符文本。在输入值之前,文本显示为浅灰色。

  • 默认值:若要在建立记录时,在字段中显示默认值,可加入此值。

  • 允许向群组显示:要限制哪些用户可以查看字段,请选择一个或多个用户访问 群组

  • 禁止向群组显示:若想防止部份用户看见此字段,可选取一个或多个 “用户访问存取 群组”。

日期时间字段属性

设定了 日期时间日期范围 小工具的 日期时间 栏位,可使用以下特别属性:

  • 时间间隔:此值决定时间选择器所显示的分钟间隔。例如:输入“15”可设定每隔 15 分钟有可选时间。预设值为 5 分钟。

  • 警告未来日期:启用此属性后,若选取了未来日期,系统会显示警告图示。

  • 显示时间日期时间 字段默认会启用此属性。对于仅读字段,停用此属性会只显示日期。举例,如此设定可使列表视图画面较简洁。

  • 最早接受日期:以 ISO 格式输入日期选择器中可选择的最早日期,即 年-月-日。如果当前日期始终是最早接受的日期,则输入 今天。在日期选择器上,最早接受日期之前的日期为灰色。

  • 最新接受日期:输入日期选择器中可选择的 ISO 格式的最新日期,即 年-月-日。如果当前日期总是最新日期,请输入 今天。在日期选择器上,晚于最新接受日期的日期将显示为灰色。

Modify a field’s properties

To modify the properties of a field, follow these steps:

  1. Navigate to the relevant view, then click the (Toggle Studio) icon in the upper-right corner. Alternatively, with any app open, click the (Toggle Studio) icon, then navigate to the relevant view.

  2. Click on the relevant field.

  3. In the Properties tab, make any desired changes to the field properties.

    小技巧

    Various properties can be configured independently for different views, such as the Label, the Widget, the visibility of the field for certain user groups, as well as whether or not the field is Invisible, Required, or Readonly.

  4. Click Close in the top-right corner to close Studio.

注解

The Technical Name of a field (visible in Studio with developer mode activated) cannot be modified in Studio; this can be done via the technical settings of the database.

Modify a field’s technical name

To modify the technical name of a field, activate developer mode, then follow these steps:

  1. With the field open in Studio, copy the technical name of the field.

  2. Remove the field from all views in Studio, then click Close to close Studio.

  3. Navigate to Settings app ‣ Technical ‣ Fields.

  4. Paste the copied technical name into the search bar, then open the relevant field.

  5. Modify the Field Name as needed.

Once the field’s technical name has been updated, the field can then be added (back) to the relevant view(s) from the list of existing fields.

重要

When updating the technical name of a field, make sure to keep at least the x_ prefix, which is required for any custom field; for fields created using Studio, keep the x_studio_ prefix to allow such fields to be more easily identified.

Remove a field from a view

To remove a field from a view, follow these steps:

  1. Navigate to the relevant view, then click the (Toggle Studio) icon in the upper-right corner. Alternatively, with any app open, click the (Toggle Studio) icon, then navigate to the relevant view.

  2. Click on the relevant field.

  3. Scroll to the bottom of the Properties tab, then click Remove from view.

  4. Click Ok to confirm the action.

小技巧

  • A removed field can be easily added back to the view at a later stage, if needed.

  • If the intention is to declutter the view, it is also possible to modify a field and make it Invisible on the view in question.