Skip to Content
Menu
This question has been flagged
1 Odpoveď
2045 Zobrazenia

I have two model, purchase.pi and purchase.order

I have order lines in purchase.pi

Now, I'm trying to get the order lines from purchase.pi and populate the order lines of purchase.order model, based on the selection of order_id. The order_id can be one or more, and order lines will be added one after another.

But, the problem is after selecting the second order_id I get its own order lines and the previously selected order line again.
Below is my code:

		​
class PurhcasePi(models.Model):
​_name="purchase.order.pi.copy"
​_inherit= ['purchase.order','mail.thread', 'mail.activity.mixin']
​_description="Purchase order copy"
​order_line= fields.One2many('purchase.order.pi.line.copy', 'order_id', string='Order Lines', states={'cancel': [('readonly', True)], 'done': [('readonly', True)]}, copy=True) ​lc_id= fields.Many2one('purchase.order', string='LC')
​def_create_lc_lines_context(self, lc_id):
​​lines= []
​for line in self.order_line:
​​if line.product_qty >0:
​​lines.append(
​​(0, 0, {
​​​'product_id': line.product_id.id,
​'name': line.name,
​'product_uom':line.product_uom,
​'price_unit':line.price_unit,
​'price_subtotal':line.price_subtotal,
​'product_qty': line.product_qty,
​                    ​​}
​                ​))
​else:
​continue
​return lines
class lc(models.Model):
​_inherit='purchase.order'

​pi_ids= fields.One2many('purchase.order.pi.copy','lc_id', string='Proforma Invoices')
​@api.onchange('pi_ids')
​def _onchange_pi_ids(self):
if self.pi_ids:
order_line=self.pi_ids._create_lc_lines_context(self.pi_ids.order_line)
​self.order_line=order_line



The marked order lines are repeated from PI00006.
Avatar
Zrušiť
Best Answer

Hi, 

We think the issue is occurring because the related records are being created again with the onchange function to resolve this, 

Try changing the initialization of the variable "lines" in function "_create_lc_lines_context" as:


              def _create_lc_lines_context(self, lc_id):                     

​ lines= [(5, 0, 0)]


Hope it helps

Avatar
Zrušiť
Autor

It worked.
Thanks for your help

Related Posts Replies Zobrazenia Aktivita
1
jan 19
3941
2
júl 20
10378
0
sep 22
2040
2
aug 19
8450
3
mar 25
928