如何从数据文件 Odoo 向 Many2one 字段添加空值

How to add null value to field Many2one from data file Odoo

我想通过数据文件添加一个新的薪资结构类别。这是我当前的代码

   <record id="MEALS" model="hr.salary.rule.category">
        <field name="name">Meals</field>
        <field name="code">MEALS</field>
        <field name="parent_id" eval=""></field>
    </record>

我只需要一个没有父类别的类别,但有了这些代码,它的父类别是新结构的基础。我试过使用 evals="0"、evals = " " 或从 中删除字段 parent_id。如何将空值添加到该字段中?

这种行为有点奇怪。据我所知,如果您不包含 parent_id 字段,它应该可以工作并具有空值,因为该字段没有默认值。 (除非您扩展了模型并在该字段上添加了 default)。

空值在 Odoo ORM 中不起作用的唯一字段是 Integerfloat 字段。 (这就是它们在表单视图中始终具有初始值 0 或 0.0 的原因)。

引用文档:

field

Each record can be composed of field tags, defining values to set when creating the record. A record with no field will use all default values (creation) or do nothing (update).

A field has a mandatory name attribute, the name of the field to set, and various methods to define the value itself:

Nothing

if no value is provided for the field, an implicit False will be set on the field. Can be used to clear a field, or avoid using a default value for the field.

试试这个 <field name="parent_id" /><field name="parent_id" eval="False" />

如果上述方法不起作用,请仔细检查您的代码库并确保您没有在该字段上设置任何默认值。