Use create method with origin value

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.

@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])

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

I was reading sale_complaint/tests/scenario_sale_complaint.rst at a05b64d2a304460a4f55b97697863b9af4845b10 · tryton/sale_complaint · GitHub and yes, in the test file, the origin is assigned in these way

complaint.origin = sale

But, in the method mencioned above I used:

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!

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