How to add / update dict fields

Hi,

Currently, I would like to put the weight into a product using product attribute.
This product is UOM = Pack/Unit. But I need to put the weight to the variant.

In the database, i can see the fields “attributes” in product.product with string something like this {“weight”:55}.

How to update this value in python?

product = Product(2)
product.attributes = ‘{“weight”: 123}’
product.save()

it give me the error

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond/model/descriptors.py", line 31, in newfunc
    return self.func(owner, [instance], *args, **kwargs)
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond/model/modelstorage.py", line 1712, in save
    (([r], save_values[r]) for r in to_write), ()))
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond_stock-5.6.0-py3.6.egg/trytond/modules/stock/product.py", line 65, in decorator
    func(cls, *args)
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond_stock-5.6.0-py3.6.egg/trytond/modules/stock/product.py", line 159, in write
    super(Product, cls).write(*args)
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond/modules/product/product.py", line 496, in write
    super().write(*args)
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond/model/modelsql.py", line 159, in wrapper
    return func(cls, *args, **kwargs)
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond/model/modelsql.py", line 980, in write
    update_values.append(field.sql_format(value))
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond/model/fields/dict.py", line 48, in sql_format
    value = super().sql_format(value)
  File "/home/coder/project/env/lib/python3.6/site-packages/trytond/model/fields/field.py", line 352, in sql_format
    value = self._py_type(value)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

You must assign a dictionary to the field:

product.attributes = {'weight': 123}

Thank @ced,

It is working

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.