# How to automatically append a carrier product line

**URL:** <https://discuss.tryton.org/t/how-to-automatically-append-a-carrier-product-line/8072>\
**Category:** Developer\
**Tags:** carrier, sales\
**Created:** [November 29, 2024, 9:52am UTC](https://discuss.tryton.org/t/how-to-automatically-append-a-carrier-product-line/8072 "2024-11-29T09:52:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jt93](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/50afbb/32.png) [@jt93](https://discuss.tryton.org/u/jt93)\
**Post date:** [November 29, 2024, 9:52am UTC](https://discuss.tryton.org/t/how-to-automatically-append-a-carrier-product-line/8072/1 "2024-11-29T09:52:44Z")

</div>

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).

```auto
    @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:

```auto
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

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [November 29, 2024, 11:45am UTC](https://discuss.tryton.org/t/how-to-automatically-append-a-carrier-product-line/8072/2 "2024-11-29T11:45:10Z")

</div>

> [@jt93](#):
>
> such that if a carrier is selected, a suitable shipping line is automatically added

Good luck.

> [@jt93](#):
>
> I’m guessing that the line object is not correctly initialized, but how could I solve it?

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

> [@jt93](#):
>
> However, this gives me the following assertion error:

It will be easier to have the full traceback.

---

<div class="post-metadata">

**Author:** ![jt93](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/j/50afbb/32.png) [@jt93](https://discuss.tryton.org/u/jt93)\
**Post date:** [November 29, 2024, 12:19pm UTC](https://discuss.tryton.org/t/how-to-automatically-append-a-carrier-product-line/8072/3 "2024-11-29T12:19:47Z")

</div>

> [@ced](#):
>
> Good luck.

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.

> [@ced](#):
>
> This is because you are accessing the `lines` attribute without depending on it.

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

> [@ced](#):
>
> It will be easier to have the full traceback.

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

```auto
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

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [November 30, 2024, 11:12am UTC](https://discuss.tryton.org/t/how-to-automatically-append-a-carrier-product-line/8072/4 "2024-11-30T11:12:54Z")

</div>

I think [https://code.tryton.org/tryton/commit/fa8f759f834b](https://code.tryton.org/tryton/commit/fa8f759f834b) 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](https://bugs.tryton.org/13716)

---

<div class="post-metadata">

**Author:** ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)\
**Post date:** [December 3, 2024, 7:25am UTC](https://discuss.tryton.org/t/how-to-automatically-append-a-carrier-product-line/8072/5 "2024-12-03T07:25:01Z")

</div>

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