Skip to Content
Menu
This question has been flagged
2 Replies
3239 Views

class SaleOrder(models.Model):

    _inherit = 'sale.order'

    

    history_ids = fields.One2many('sale.order','odr_id')

    odr_id = fields.Many2one('sale.order')

    

    @api.onchange('partner_id')

    def onchange_partner_id(self):

        res = super(SaleOrder, self).onchange_partner_id()

        order_ids = self.env['sale.order'].search([('partner_id', '=', self.partner_id.id)])

 


        order_history = []

        for record in order_ids:

            if record.state in ('sale', 'done'):

                order_history.append((0, 0, {

                                      'name': record.name,

                                      'amount_total': record.amount_total, 

                                      'state': record.state,

                                      'user_id': record.user_id.id, 

                                      'confirmation_date': record.confirmation_date,

                                      'invoice_status': record.invoice_status

                                      }))

        if order_history:

            self.update({'history_ids': order_history})

            

        return res

##############################################

This is my code.There is a one2many relation to sale order.

I am using this module for save the previous history of customers.

When save the sale order there is a warning will be happend.

How to solve this issue??


Avatar
Discard
Author

i change my code like this

# program

class SaleOrder(models.Model):

_inherit = 'sale.order'

history_ids = fields.One2many('sale.order','odr_id')

odr_id = fields.Many2one('sale.order')

@api.onchange('partner_id')

def onchange_partner_id(self):

res = super(SaleOrder, self).onchange_partner_id()

order_ids = self.env['sale.order'].search([('partner_id', '=', self.partner_id.id),

('state', 'not in', ['draft', 'sent'])])

self.create({'history_ids': [(6, 0, order_ids)]})

return res

Notes

---------

Get an attribute error on delete(name). How to solve these issue.?

the model sale order and i want to insert values to the same model.so don't need to append like (0,0). I think (6,0, ids) is correct. Please help me how to solve this issue??

Best Answer

Still, your res contains the return value of the original method. Please try update res as

if order_history:
res.update({
'history_ids': order_history
})
Avatar
Discard
Best Answer

Hello,

If I have good understanding, you would create a One2Many line in the onchange.

You can do this:

class SaleOrder(models.Model):
 _inherit = 'sale.order'

history_ids = fields.One2many('sale.order','odr_id')
odr_id = fields.Many2one('sale.order')

@api.onchange('partner_id')
def onchange_partner_id(self):
res = super(SaleOrder, self).onchange_partner_id()
order_ids = self.env['sale.order'].search([('partner_id', '=', self.partner_id.id)])

order_history = []
for record in order_ids:
if record.state in ('sale', 'done'):
order_history.append((0, 0, {
'name': record.name,
'amount_total': record.amount_total,
'state': record.state,
'user_id': record.user_id.id,
'confirmation_date': record.confirmation_date,
'invoice_status': record.invoice_status
}))
if order_history:
self.history_ids = order_history
return res
Avatar
Discard