Odoo | 技巧 | 字段显示-关系字段的显示值自定义

本文介绍如何在Odoo中自定义关系字段的显示值。通过简单地修改模型的_rec_name属性,可以轻松更改关系字段的显示内容。例如,可以在hr.employee.category模型中添加description字段,并将其设置为_rec_name,使得在hr.employee视图中搜索员工标签时显示description的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How to change displayed value for relational field in Odoo

If you want to change the displayed value for a relational field in Odoo, the simplest way to do that is to define the “_rec_name” model’s attribute with the name of the field that you want to show.

For example, if you want to add new field “description” in the hr.employee.category model and you want that field to be shown as value for this model the code should look like:

class Employee(models.Model):
    _inherit = 'hr.employee.category'
    _rec_name = 'description'

    description = fields.Char()

Now when you try to search for a new employee tag in the hr.employee view it will search by and display the description of the hr.employee.category model.

简而言之,简单的改变关系字段的显示,只需要将对应关系字段的model中_rec_name修改成需要显示的字段即可,这样之后的字段显示就是该字段的值。

From:http://www.odooninja.com/change-displayed-value-relational-field-odoo/

- 2019年12月07日11:01:20 更新

1. 默认显示name字段

2. 如果定义了_rec_name则显示对应的字段值

3. 可以重写name_get方法,通过传入的context来个性化显示字段内容

Odoo中添加自定义字段并在报表中使用是一个常见的需求,特别是在需要展示特定业务数据的情况下。以下是详细的操作步骤: ### 添加自定义字段 1. **继承模型并添加字段** 在Odoo中,可以通过继承现有模型来添加自定义字段。例如,如果希望在`res.partner`模型中添加一个名为`custom_field`的字段,可以在模块的Python文件中进行如下操作: ```python from odoo import models, fields class ResPartner(models.Model): _inherit = 'res.partner' custom_field = fields.Char(string="Custom Field") ``` 2. **更新视图以显示字段** 在XML文件中,修改或创建视图(如表单视图)以显示新增的自定义字段。例如: ```xml <record id="view_partner_form_custom" model="ir.ui.view"> <field name="name">res.partner.form.custom</field> <field name="model">res.partner</field> <field name="inherit_id" ref="base.view_partner_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='display_name']" position="after"> <field name="custom_field"/> </xpath> </field> </record> ``` 3. **安装或升级模块** 确保将包含上述代码的模块安装或升级,以便应用更改。 ### 在报表中使用自定义字段 1. **编辑报表模板** Odoo中的报表通常使用QWeb模板进行渲染。可以通过继承现有的报表模板,并在其中添加新字段。例如,在XML文件中添加以下内容: ```xml <template id="report_partner_document_custom" inherit_id="base.report_partner_document"> <xpath expr="//div[@class='page']" position="inside"> <p>Custom Field: <span t-field="partner.custom_field"/></p> </xpath> </template> ``` 2. **生成报表** 完成上述步骤后,进入Odoo界面,导航到相关记录并尝试生成报表,此时可以看到新增的自定义字段已经出现在报表中。 3. **调试与测试** 如果字段未正确显示,请检查模型、视图和模板是否正确关联,并确保字段的数据已正确填写。 通过以上步骤,可以成功地在Odoo中添加自定义字段并在报表中使用这些字段[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比特本特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值