Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda

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
Buang
Jawaban Terbai

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
Buang
Penulis

It worked.
Thanks for your help

Post Terkait Replies Tampilan Aktivitas
1
Jan 19
4097
2
Jul 20
10602
0
Sep 22
2110
2
Agu 19
8599
3
Mar 25
1089