【问题标题】:many2many fileds are both are same values?many2many 字段都是相同的值?
【发布时间】:2017-01-23 13:50:28
【问题描述】:

我有这个代码:

在 .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)])

在 .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)]}"/>

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

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

【问题讨论】:

    标签: python postgresql openerp


    【解决方案1】:

    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
        )
    

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

    sale_order_line_account_tax_rel

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

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

    【讨论】:

    • 最好按照 CZoeliner 的建议为 Many2Many 提供可选值! ... Odoo 是开源的,不保证这种自动决策不会导致任何问题...所以在某些情况下最好避免 odoo 行为不端...
    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    相关资源
    最近更新 更多