many2many字段都是相同的值吗?

Shi*_*uku 2 python postgresql openerp

我有以下代码:

在.py文件中:

class newsaleorderline(models.Model):
    _inherit='sale.order.line'

    supply_tax_id = fields.Many2many('account.tax',string='Supply Taxes',domain=['|', ('active', '=', False), ('active', '=', True)])         
    labour_tax_id = fields.Many2many('account.tax',string='Labour Taxes',domain=['|', ('active', '=', False), ('active', '=', True)])
Run Code Online (Sandbox Code Playgroud)

在.xml文件中:

<field name="supply_tax_id" widget="many2many_tags" domain="[('type_tax_use','=','sale'),('company_id','=',parent.company_id)]" attrs="{'readonly': [('qty_invoiced', '&gt;', 0)]}"/>
<field name="labour_tax_id" widget="many2many_tags" domain="[('type_tax_use','=','sale'),('company_id','=',parent.company_id)]" attrs="{'readonly': [('qty_invoiced', '&gt;', 0)]}"/>
Run Code Online (Sandbox Code Playgroud)

虽然我尝试更改supply_tax_id它,但是保存后supply_tax_idlabour_tax_id两者是相同的。我不知道它是如何相互连接的。我想要supply_tax_id并且labour_tax_id应该是不同的值,字段应该来自account.tax

请帮我找到解决问题的方法。谢谢大家的建议。

CZo*_*ner 5

Odoo正在数据库中生成关系表。您可以在字段定义中自行指定表名称:

class MyModel(models.Model):
    _name = "my.model"

    my_m2m_field = fields.Many2Many(
        comodel_name="another.model", # required
        relation="my_model_another_model_rel", # optional
        column1="my_model_id", # optional
        column2="another_model_id", # optional
        string="Another Records" # optional
    )
Run Code Online (Sandbox Code Playgroud)

您的示例未relation在字段定义中使用参数,因此Odoo会自行生成名称。它使用两个模型(表)名称,并在名称末尾添加“ _rel”:

sale_order_line_account_tax_rel

问题在这里:您在两个不同的Many2Many字段上使用相同的两个模型,这些字段最终将出现在一个关系表中。因此,在使用字段时,两个字段将在客户端中表示相同的数据。

解决方案:使用参数relation并为关系表定义两个不同的名称。