Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
2057 Lượt xem

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.
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

It worked.
Thanks for your help

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 1 19
3945
2
thg 7 20
10385
0
thg 9 22
2040
2
thg 8 19
8452
3
thg 3 25
930