Compute method failed to assign product.template . Odoo 14

Viewed 852

I got this error when trying to add compute to field in smart button added into 'product.template' model.

 File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1026, in
    __get__
        raise ValueError("Compute method failed to assign %s.%s" % (record, self.name)) Exception
    
The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "/usr/lib/python3/dist-packages/odoo/http.py", line 639, in
_handle_exception
    return super(JsonRequest, self)._handle_exception(exception)   File "/usr/lib/python3/dist-packages/odoo/http.py", line 315, in
_handle_exception
    raise exception.with_traceback(None) from new_cause ValueError: Compute method failed to assign product.template(33,).qty

What's wrong please? Any help ?? Here is my code:

 <record id="view_product_test" model="ir.ui.view">
        <field name="name">view_product_test</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view"/>
        <field name="arch" type="xml">
            <xpath expr="//button[@name='action_open_quants']" position="before">
                 <button class="oe_stat_button"
                               name="my_action"
                               icon="fa-cubes"
                               type="object" attrs="{'invisible':[('type', '!=', 'product')]}">
                               <div class="o_field_widget o_stat_info">
                                    <span class="o_stat_value">
                                        <field name="qty" widget="statinfo" nolabel="1" class="mr4"/>
                                    </span>
                                    <span class="o_stat_text">Qty</span>
                               </div>
                        </button>
            </xpath>
        </field>
    </record>


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

    qty = fields.Float('Qty', compute='test')

    def test(self):
        _logger.info('--------------')
1 Answers

You need assignments in your compute method. What did you expect Odoo to do, when the method is empty?

Atleast provide something:

def test(self):
    for record in self:
        record.qty = 0.0

Edit: and yes, the error message isn't the best one by Odoo.

Related