如何从Odoo中的产品模板属性行创建产品变体?

时间:2019-01-09 11:26:58

标签: python python-2.7 odoo odoo-10

我正在使用Odoo 10,并且创建了一组产品模板。当我启用产品变体时,每个产品模板都生成了一个产品变体。然后,我导入了一个CSV文件,该文件将产品属性行添加到产品模板中。由于产品模板现在具有各自的属性行,因此效果很好。

由于我已经导入了这些属性行,而不是手工创建它们,因此它们没有生成任何产品变体。例如,我已导入模板的属性行 Size S M L 值> T恤。我希望Odoo删除单个 T-Shirt 变体并生成变体 T恤S T恤M T -shirt L (将属性或属性值添加到模板时的行为),但没有执行任何操作。

所以我试图通过代码生成它们,为此我使用了shell。首先,我搜索我创建的模板。将它们存储在名为templates的变量中后,我对模板记录集执行create_variant_ids

>>> templates = self.env['product.template'].search([('create_date', '>=', '2019-01-06 00:00:00')])
>>> templates.create_variant_ids()

结果是这样的:

2019-01-09 10:33:00,993 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1052]
2019-01-09 10:33:01,782 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1061]
2019-01-09 10:33:02,573 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1054]
2019-01-09 10:33:03,353 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1055]
2019-01-09 10:33:04,166 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1056]
2019-01-09 10:33:05,000 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1057]
2019-01-09 10:33:05,828 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1058]
2019-01-09 10:33:06,621 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1053]
2019-01-09 10:33:07,438 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1059]
2019-01-09 10:33:08,241 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1060]
2019-01-09 10:33:09,051 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [1051]
2019-01-09 10:33:09,841 2775 INFO trivi_v10 odoo.models.unlink: User #1 deleted product.product records with IDs: [956]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/odoo/odoo_10/src/linked/product/models/product_template.py", line 403, in create_variant_ids
    'attribute_value_ids': [(6, 0, variant_ids.ids)]
  File "/opt/odoo/odoo_10/src/linked/product/models/product.py", line 322, in create
    product = super(ProductProduct, self.with_context(create_product_product=True)).create(vals)
  File "/opt/odoo/odoo_10/src/linked/mail/models/mail_thread.py", line 228, in create
    thread = super(MailThread, self).create(values)
  File "/opt/odoo/odoo_10/src/oca/OCB/odoo/models.py", line 3847, in create
    record = self.browse(self._create(old_vals))
  File "/opt/odoo/odoo_10/src/oca/OCB/odoo/models.py", line 4002, in _create
    self._validate_fields(vals)
  File "/opt/odoo/odoo_10/src/oca/OCB/odoo/models.py", line 1078, in _validate_fields
    check(self)
  File "/opt/odoo/odoo_10/src/linked/product/models/product.py", line 310, in _check_attribute_value_ids
    raise ValidationError(_('Error! It is not allowed to choose more than one value for a given attribute.'))
ValidationError: (u'Error! It is not allowed to choose more than one value for a given attribute.', None)

我认为ID 956的产品变体会有问题,但与其余变体的数据相似。

有人知道如何(通过代码或界面)创建产品变体吗?

1 个答案:

答案 0 :(得分:1)

已解决。

问题在于导入文件是错误的,因为它具有与同一产品模板相关联的相同产品属性行多次。

例如,产品模板 T恤有两行 Size

一旦我更正了导入文件的重复项,就可以使用Shell毫无问题地执行create_variant_ids

顺便说一句,如果您不希望通过Shell进行操作,则在导入产品属性行之后通过界面重新计算变体的变通办法是将产品模板存档,然后将其取消存档。

如果您过滤以仅查看要更新的产品模板,在树视图中显示并选择所有产品模板,然后单击 Action> Archive ,然后添加搜索过滤器< em> Unarchived ,将其全部选中,然后单击 Action> Unarchive 。此过程将重新计算所有变体,从而从导入的产品属性行中创建新的变体。