跳至内容
菜单
此问题已终结
1 回复
2595 查看

Hi everyone, I've been trying to point to specific index in the output of my browse method.


this is my code:

def unir_facturas_boton(self, vals):
active_invoices = self.env['account.move']._context.get('active_ids', [])
active_records = self.env['account.move'].browse(active_invoices)
# source = active_records[--1]
# source_2 = active_records[0]

for items in active_records:
print(items)
# if items[-1].partner_id != items[0].partner_id:
# raise ValidationError('No es posible unir facturas con clientes diferentes')
# else:
# invoice = self.env['account.move'].create(
# {
# 'company_id': items[-1].company_id,
# 'type': items[-1].type,
# 'invoice_date': items[-1].invoice_date,
# 'partner_id': items[-1].partner_id.id,
# 'currency_id': items[-1].currency_id.id,
# 'journal_id': 11
# })
# for lines in items[-1].invoice_line_ids:
# invoice.write({
# "invoice_line_ids": items[0].invoice_line_ids.append(lines)


# })

the commented part is giving me erros since I cant catch the index.



this the output of the browse method:

account.move(2,)
account.move(1,)


how can I point to an specific one of these two?



形象
丢弃
最佳答案

It looks like you are trying to merge invoices and don't want to merge them if they each have a different partner_id.  My question would be: are you selecting invoices that have the same partner_id?

As for the error, I think you're using your for loop syntax incorrectly.  It might help to instead write it as:

for item in active_records:

Each item will be iterated through, and depending on the type of object they may have elements 0 and -1, but it's unlikely.  You would want to check active_records[0] against active_records[-1] for a match on the partner_id.  At least that's how it makes sense in my head.

Here's a link on how that particular function works in case you'd like a refresher:
https://www.geeksforgeeks.org/iterate-over-a-list-in-python/

Hope that helps.

形象
丢弃
编写者

Yes, I just solved it doing the same thing, instead of iterating over the values of active_record I just created to variables that store each index, In my case I'll always just use 2 active_ids so it would be only active_record[0] and active_record[1], but now I'm having issues trying to append the invoice_line_ids from record[0] with record[1] and making a new invoice based on this

Not sure if this will solve your new issue, but I would think you'd want to create the new invoice object first and append the invoice_line_ids to that new object. Alternatively you could just use one of the existing ones and append the ones from the other prior to archiving it.

相关帖文 回复 查看 活动
4
2月 24
6395
3
3月 24
2816
0
11月 22
2015
2
2月 22
5585
0
11月 21
2586