Skip to Content
Menu
This question has been flagged
3 Replies
3721 Views

Hello,


I'm beginner and didn't find how to do probably a simple thing in Odoo.

I have my class A with :

class PurchOrderLine(models.Model):
_inherit = 'purchase.order.line'

history = fields.Many2one('app.purch.order.line.copy', string='Historique')

@api.onchange('product_id')
def onchange_partner_id(self):
print 'oooooooooooooooooo \n'
history = dict()
for i in self.search([('product_id', '=', self.product_id.id)]):
history[i.date_order] = i.product_qty
print history

History gives me values according to a product id, so if I select the product id 1 on this line it returns values like this ['date_order of product 1' : 'product quantity'] , [ ...] , ...


I want to display a Many2one field that i put on another class :

class PurchOrderLineCopy(models.Model):
_name = 'app.purch.order.line.copy'
_rec_name = 'history'
history = fields.Char(string='Historique')


But I really don't know how to push values. I tried to use self.env['app.purch.order.line.copy'].create(history) but that's not the good solution.


Thx

Avatar
Discard
Best Answer

I think you should create One2many field in purchase.order.line instead of many2one.

class PurchOrderLine(models.Model):
_inherit = 'purchase.order.line'

history = fields.One2many('app.purch.order.line.copy', 'po_line_id',
'Historique')


class PurchOrderLineCopy(models.Model):
_name = 'app.purch.order.line.copy'
_rec_name = 'history'

po_line_id = fields.Many2one('purchase.order.line', 'PO Line')
history = fields.Char(string='Historique')


# Then use following code in your onchange
self.env['purchase.order.line'].write((0, 0, {'history': 'ABCD'}), (0, 0, {'history': 'XYZ'}))


Avatar
Discard
Best Answer
    @api.onchange('product_id')
def onchange_partner_id(self):
print 'oooooooooooooooooo \n'
history = dict()
for i in self:
history[i.date_order] = i.product_qty
print history
Avatar
Discard
Author Best Answer

No it doens't work with only self.

I need to link the values to my product id.


And print of history gives me something like that :

{'2020-10-16 12:21:46': 9.0, '2020-10-14 13:21:56': 6.0}

That mean when i chose the product id 1 in the first line of the purchase order i want to display this values in my Many2one field (they are the 2 last orders).

When i try to do :
self.env['app.purch.order.line.copy'].write((0, 0, history))


or
self.env['app.purch.order.line.copy'].write({'history': [[0, 0, {'name': 'ok'}]]})
nothing happend

Avatar
Discard
Related Posts Replies Views Activity
1
May 20
2800
1
Mar 24
893
1
Mar 23
4292
1
Nov 22
14647
1
Sep 22
949