简体   繁体   English

如何使用 XML 文件向 Odoo 中的 Many2many 字段添加记录?

[英]How to add a record to Many2many field in Odoo using XML file?

I am trying to create a record in a many2many field in account.account model.我正在尝试在account.account model 的 many2many 字段中创建记录。 I created a Many2many field in account.account model, which is related to mapped.fields model.我在account.account model中创建了一个Many2many字段,它与mapped.fields model相关。 Now I need to create a record for a COA using xml data.现在我需要使用xml数据为 COA 创建记录。 Here is what I tried.这是我尝试过的。

*.xml *.xml

      <record id="coa_field_a_10rf_annual_rev" model="coa.field.mapping">
        <field name="name" ref='mof.field_cit_a_10rf_annual_rev'/>
        <field name="cit_method">03</field>
      </record> <!-- Here a record has been created in the model but not related to its parent field. -->

      <record model="account.account" id="l10n_qa.1_bank_template">
        <!-- Method1 -->
     <field name="existing_coa_fields_ids" eval="[
       (6,0,[ref('mof.coa_field_a_10rf_annual_rev')])
       ]"/>

       <!-- Method 2 -->
     <field name="existing_coa_fields_ids" eval="[
       (0, 0, [ {'name':ref('mof.field_cit_a_10rf_annual_rev'),'cit_method':'01'}] ),
       (0, 0, [ {'name':ref('mof.field_cit_c_96sm_net_taxable_inc'),'cit_method':'03'}] ),
       ]"/>
    </record>

How can I resolve this?我该如何解决这个问题? Note: the external_id l10n_qa.1_bank_template which is created by Odoo itself.注意:由 Odoo 自己创建的 external_id l10n_qa.1_bank_template

The l10n_qa.1_bank_template account is marked with noupdate , you can check it under Technical / Sequences & Identifiers / External Identifiers . l10n_qa.1_bank_template账户标有noupdate ,您可以在Technical / Sequences & Identifiers / External Identifiers下查看。

You cannot update such a record with the above code, To set the value of existing_coa_fields_ids , you can use a function您无法使用上述代码更新此类记录,要设置existing_coa_fields_ids的值,您可以使用function

In the following example, we use the record ID to call the write function on the account.account model using the values provided by value tag (passed as second parameter):在以下示例中,我们使用记录 ID 使用value tag 提供的值(作为第二个参数传递)调用account.account model 上的write function:

<function model="account.account" name="write">
     <value model="ir.model.data" eval="obj().env.ref('l10n_qa.1_bank_template').id"/>
     <value eval="{'existing_coa_fields_ids': [Command.set([ref('mof.coa_field_a_10rf_annual_rev')])]}"/>
</function>

You can also set the noupdate flag to 0 to update data then set it again to 1您还可以将noupdate标志设置为0以更新数据,然后再次将其设置为1

Example:例子:

<function model='ir.model.data' name="toggle_noupdate">
    <value eval="'account.account'"/>
    <value model="ir.model.data" eval="obj().env.ref('l10n_qa.1_bank_template').id"/>
</function>
    
<record model="account.account" id="l10n_qa.1_bank_template">
    <field eval="[Command.set([ref('mof.coa_field_a_10rf_annual_rev')])]" name="existing_coa_fields_ids"/>
</record>
  
<function model='ir.model.data' name="toggle_noupdate">
    <value eval="'account.account'"/>
    <value model="ir.model.data" eval="obj().env.ref('l10n_qa.1_bank_template').id"/>
</function>

Odoo encourages developers to use Command to set the x2many field values Odoo 鼓励开发者使用Command设置 x2many 字段值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM