# Use create method with origin value

**URL:** <https://discuss.tryton.org/t/use-create-method-with-origin-value/1889>\
**Category:** Developer\
**Created:** [October 31, 2019, 11:25pm UTC](https://discuss.tryton.org/t/use-create-method-with-origin-value/1889 "2019-10-31T23:25:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [October 31, 2019, 11:25pm UTC](https://discuss.tryton.org/t/use-create-method-with-origin-value/1889/1 "2019-10-31T23:25:05Z")

</div>

Hi,

I’m creating a custom method in sale.sale called credit\_sale. I’m not sure how to fill origin correctly. I’m trying with the code below, but I give the message “can’t adapt type ‘sale.sale’”. Any help will be appreciated.

```auto
@classmethod
def credit_sale(cls, sale):
        pool = Pool()
        Complaint = pool.get('sale.complaint')
        ComplaintType = pool.get('sale.complaint.type')
        ComplaintAction = pool.get('sale.complaint.action')
        ActionSaleLine = pool.get('sale.complaint.action-sale.line')
        Sale = pool.get('sale.sale')

        types = ComplaintType.search([])
        if len(types)==1:
            type_ = types[0].id

        sales = Sale(sale)

        complaint, = Complaint.create([{
                    'type': type_,
                    'customer': sale.party.id,
                    'origin': sales,
                }])

        complaint_action, = ComplaintAction.create([{
            'complaint':complaint.id,
            'action':'sale_return',
            }])
        complaint.wait([complaint])
        Complaint.approve([complaint])

```

---

<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 1, 2019, 9:23am UTC](https://discuss.tryton.org/t/use-create-method-with-origin-value/1889/2 "2019-11-01T09:23:48Z")

</div>

You can not use instance as value of `ModelStogare.create` calls. You must use their integer (`id`) or you must use an instance that you will save using `ModelStorage.save`. The later is the preferred because it allow better customization (e.g. returning unsaved instance to allow other module to assign their own fields).

---

<div class="post-metadata">

**Author:** ![iehoshia](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/iehoshia/32/243_2.png) [@iehoshia](https://discuss.tryton.org/u/iehoshia)\
**Post date:** [November 1, 2019, 3:58pm UTC](https://discuss.tryton.org/t/use-create-method-with-origin-value/1889/3 "2019-11-01T15:58:30Z")

</div>

> [@ced](#):
>
> `save`

I was reading [sale\_complaint/tests/scenario\_sale\_complaint.rst at a05b64d2a304460a4f55b97697863b9af4845b10 · tryton/sale\_complaint · GitHub](https://github.com/tryton/sale_complaint/blob/a05b64d2a304460a4f55b97697863b9af4845b10/tests/scenario_sale_complaint.rst) and yes, in the test file, the origin is assigned in these way

> > > complaint.origin = sale

But, in the method mencioned above I used:

```auto
types = ComplaintType.search([('name','=','Sale')])
        if len(types)==1:
            type_ = types[0].id
            types_ = types[0]

origin = str(types_.origin.model) + ',' + str(sale.id)

complaint, = Complaint.create([{
                    'type': type_,
                    'customer': sale.party.id,
                    'origin': origin,
                }])

```

I’m not sure how it works, but works fine. Let me know if there is a better way to do the same.

Thanks for the help!

---

<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 1, 2019, 3:58pm UTC](https://discuss.tryton.org/t/use-create-method-with-origin-value/1889/4 "2019-12-01T15:58:33Z")

</div>

Closed on December 1, 2019, 3:58pm UTC

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