OCA / odoorpc

Python module to pilot your Odoo servers through JSON-RPC.
http://pythonhosted.org/OdooRPC/
GNU Lesser General Public License v3.0
231 stars 124 forks source link

Wrong number of arguments when calling on_change method #18

Closed agrings closed 5 years ago

agrings commented 6 years ago

Hello,

Im trying to call the following onchange method:

@api.onchange('fiscal_category_id',
                  'fiscal_position',
                  'invoice_line_tax_id',
                  'quantity',
                  'price_unit',
                  'discount',
                  'insurance_value',
                  'freight_value',
                  'other_costs_value')
    def onchange_fiscal(self):
        ctx = dict(self.env.context)
        if self.invoice_id.type in ('out_invoice', 'out_refund'):
            ctx.update({'type_tax_use': 'sale'})
        else:
            ctx.update({'type_tax_use': 'purchase'})

        partner_id = self.invoice_id.partner_id.id or ctx.get('partner_id')
        company_id = self.invoice_id.company_id.id or ctx.get('company_id')
        if company_id and partner_id and self.fiscal_category_id:
            result = {'value': {}}
            kwargs = {
                'company_id': company_id,
                'partner_id': partner_id,
                'partner_invoice_id': self.invoice_id.partner_id.id,
                'product_id': self.product_id.id,
                'fiscal_category_id': self.fiscal_category_id.id,
                'context': ctx
            }
            result = self.with_context(ctx)._fiscal_position_map(
                result, **kwargs)

            kwargs.update({
                'invoice_line_tax_id': [
                    (6, 0, self.invoice_line_tax_id.ids)],
                'quantity': self.quantity,
                'price_unit': self.price_unit,
                'discount': self.discount,
                'fiscal_position': self.fiscal_position.id,
                'insurance_value': self.insurance_value,
                'freight_value': self.freight_value,
                'other_costs_value': self.other_costs_value,
            })
            result['value'].update(self._validate_taxes(kwargs))
            self.update(result['value'])

So i used the suggestion given in the docs:

def on_change(record, method, args=None, kwargs=None):
    """Update `record` with the result of the on_change `method`"""
    res = record._odoo.execute_kw(record._name, method, args, kwargs)
    for k, v in res['value'].iteritems():
        setattr(record, k, v)

And called the function:

on_change(invoiceLine, 'onchange_fiscal', [],{})

But I'm getting the error:

onchange_fiscal() takes exactly 1 argument (3 given)
sebalix commented 6 years ago

Well, the example in the documentation was for old on_change methods (old api) not new ones. For api.onchange(...) methods, you have to call the generic method onchange(...) on your record (link).

For instance, if I change the date_begin value of an event.event record, I want to get back the corresponding date_end (link):

>>> event = odoo.env['event.event'].browse(5)
>>> event.onchange({'date_begin': u"2018-01-31 00:00:00", 'date_end': False}, 'date_begin', {'date_begin': '1'})
{u'value': {u'date_end': u'2018-01-31 01:00:00'}}

This part of the documentation needs to be improved to explain both types of onchange, thank you for the report.

agrings commented 6 years ago

Thank you for the quick reply

pedrobaeza commented 5 years ago

Closing as seems answered.