How to automatically append a carrier product line

Hi,
I tried to extend the Sale module such that if a carrier is selected, a suitable shipping line is automatically added (staying in draft state without quotation).

    @fields.depends('carrier')
    def on_change_carrier(self):
        pool = Pool()
        SaleLine = pool.get('sale.line')

        if self.carrier:
            line = SaleLine(type='line')
            line.sale = self
            line.product = self.carrier.carrier_product
            line.quantity = 1
            line.on_change_product()

            self.lines = self.lines + (line,)

However, this gives me the following assertion error:

assert hasattr(init_record, fname), (init_record, fname)
AssertionError: (Pool().get('sale.line')(**{}), 'taxes')

I’m guessing that the line object is not correctly initialized, but how could I solve it?

Thanks

Good luck.

This is because you are accessing the lines attribute without depending on it.

It will be easier to have the full traceback.

What I meant is adding the default carrier product with standard price which in my case is mostly sufficient and if not it can easily be adjusted.

I tried to add lines to the decorator but get the same error.

This happens when I call the on_change_product() as above:

File "/home/tryton/trytond/trytond/protocols/wrappers.py", line 208, in wrapper
    result = func(request, pool, *args, **kwargs)
  File "/home/tryton/trytond/trytond/protocols/dispatcher.py", line 221, in _dispatch
    result = rpc.result(meth(inst, *c_args, **c_kwargs))
  File "/home/tryton/trytond/trytond/model/fields/field.py", line 195, in on_change_result
    return record._changed_values
  File "/home/tryton/trytond/trytond/model/modelview.py", line 919, in _changed_values
    added_values = target._changed_values
  File "/home/tryton/trytond/trytond/model/modelview.py", line 891, in _changed_values
    assert hasattr(init_record, fname), (init_record, fname)

Thanks for your help

I think Assert that xxx2many fields have an initial value to compute changes (fa8f759f834b) · Commits · Tryton / Tryton · GitLab did not take into account the creation of instance.
I think we should not assert if the id is None: Can not call change functions that modify xxx2Many on new instance (#13716) · Issues · Tryton / Tryton · GitLab

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