【问题标题】:Duplicate record missing many2one field issue重复记录缺少 many2one 字段问题
【发布时间】:2017-11-23 16:57:45
【问题描述】:

我的产品带有来自自定义模块的额外字段。我正在尝试重写复制功能,否则当我尝试复制产品时,某些值不会重复。

因此,在此复制功能中,我设法添加了这些字段,但如果我尝试仅添加原始产品字段中的 id,则在复制产品后该值会丢失。我想这是有道理的。

代码:

class ProductTemplate(models.Model):
    _inherit= 'product.template'

    @api.multi
    def copy(self, default=None):
        default = dict(default or {})
        array_attribute_line_ids = []
        for value_id in self.attribute_line_ids.read([]):
            array_attribute_line_ids += [(4,value_id['id'])]
        print (array_attribute_line_ids)
        default.update({
            # General Information
            # 'standard_price': self.standard_price,

            # Variants > Attributes
            'attribute_line_ids': array_attribute_line_ids,
            # Purchase > Vendors

            # Inventory
            'weight': self.weight,
            'volume': self.volume,

            # Invoicing
            'property_account_income_id': self.property_account_income_id,
            'supplier_taxes_id': self.supplier_taxes_id, #NOT WORKING
            'property_account_expense_id': self.property_account_expense_id,
            'property_account_creditor_price_difference': self.property_account_creditor_price_difference,
        })
        return super(ProductTemplate, self).copy(default)

查看:手头的字段是图片底部显示的attribute_line_ids。

问题:我可以在原始记录的基础上创建新记录,但问题是这些值之一取决于需要值“product_tmpl_id”的 many2one,即产品的 id。那么,如果我还没有新产品的 id,因为它目前还没有被复制,我该如何创造这些价值。

之前在不同的功能中我创建了记录,然后使用它的 id 更新这些值。现在不可能了。

有没有办法解决这个问题?是否有一个 .copy() 函数可以让我复制所有这些记录而无需手动创建?

谢谢你。

这是 odoo 11 企业版。

【问题讨论】:

    标签: python openerp odoo-10 odoo-11


    【解决方案1】:

    IIUC 您希望在复制产品时复制产品属性。我相信您可以通过将product.template 属性copy 上的字段attribute_line_ids 更改为True 来实现这一点:

    from odoo import fields, models
    
    
    class ProductTemplate(models.Model):
        _inherit = 'product.template'
    
        attribute_line_ids = fields.One2many(copy=True)
    

    【讨论】:

    • 我试过了,但没有用。有什么建议吗?我仍然有同样的行为。
    • 您是否正在更改default 字典中product.template copy 方法中attribute_line_ids 的值?因为您不需要使用我描述的解决方案来执行此操作。我在本地对其进行了测试,它对我有用(复制产品时会复制产品属性)。
    • 是的,你是对的。现在可以了。我真的很感谢你的帮助。这让我很头疼。
    猜你喜欢
    • 2018-04-16
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多