简体   繁体   中英

How to create product variants from the product template attribute lines in Odoo?

I am using Odoo 10 and I have created a group of product templates. As I have product variants enabled, each product template has generated a product variant. Then, I have imported a CSV file which adds product attribute lines to the product templates. It worked well as product templates have now their respective attribute lines.

Since I have imported these attribute lines instead of creating them by hand, they generated no product variant. For example, I have imported the attribute line Size with S , M , L values for the template T-shirt . I expected Odoo to remove the single T-Shirt variant and generate the variants T-shirt S , T-shirt M and T-shirt L (which is the behaviour when you add an attribute or an attribute value to the template), but it did nothing.

So I am trying to generate them through code, I am using the shell for that. First I search for the templates I have created. Once I have them stored in a variable named templates , I execute the create_variant_ids to the templates recordset:

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

The result is this one:

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)

I thought that product variant with ID 956 will have something wrong, but it has similar data to the rest of variants.

Does anyone know how to create the product variants (through code or interface)?

Solved.

The problem was that the importation file was wrong, as it had the same product attribute line associated to the same product template more than once.

For example, there were two lines of Size for the product template T-shirt .

Once I have corrected the duplicates of the importation file, I was able to use the shell to execute create_variant_ids with no problem.

By the way, if you do not want to do it by the shell, a workaround to recalculate the variants through the interface after importing the product attribute lines would be to archive your product templates and then unarchiving them.

It is very fast if you filter to see only the product templates to update, show and select all of them in the tree view, click on Action > Archive , an then add the search filter Unarchived to yours, select them all again and click on Action > Unarchive . This process recalculates all the variants and thus creates my new ones from the imported product attribute lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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