odoo在文本字段中获取多个数据

odoo get several data in text field

我有这个 py 脚本

@api.one
@api.depends('customer_id')
def _get_unpaid_list(self):
    invoice_pool = self.env['account.invoice']
    for rec in invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', self.customer_id.id)]):
        amount_total = rec.amount_total or ''
        number = rec.number or ''
        name = ustr(amount_total) + "-" + ustr(number)
        self.unpaid_list = name
unpaid_list = fields.Text(string="Customer Debt List", compute="_get_unpaid_list", )

和这个xml

<field name="unpaid_list" />

它适用于此输出

19690000.0-INV/2021/13928

但它应该像这样显示几行数据

19690000.0-INV/2021/13928
25590000.0-INV/2021/13929
23450000.0-INV/2021/13930

如何按应有的方式制作数据列表? 谢谢

在循环内使用字符串连接。生成换行符分隔的完整字符串,然后将其写入字段

@api.one
@api.depends('customer_id')
def _get_unpaid_list(self):
    invoice_pool = self.env['account.invoice']
    unpaid_list = ''
    for rec in invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', self.customer_id.id)]):
        amount_total = rec.amount_total or ''
        number = rec.number or ''
        name = ustr(amount_total) + "-" + ustr(number)
        if len(unpaid_list)>0:
            unpaid_list += '\n'
        unpaid_list += name
    self.unpaid_list = unpaid_list

unpaid_list的值设置为循环中的最后一个name。要解决此问题,您需要在 for 循环之外设置字段值。

示例:

@api.depends('customer_id')
def _get_unpaid_list(self):
    invoice_pool = self.env['account.invoice']
    for record in self:
        name = ''
        for invoice in invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', record.customer_id.id)]):
            amount_total = invoice.amount_total or ''
            number = invoice.number or ''
            name += ustr(amount_total) + "-" + ustr(number) + "\n"
        record.unpaid_list = name  

您可以使用计算的 One2many 字段来显示未付发票的列表并让用户打开(发票表单)并查看发票详细信息。

示例:

@api.depends('customer_id')
def _get_unpaid_invoices(self):
    invoice_pool = self.env['account.invoice']
    for record in self:
        record.unpaid_list_ids = invoice_pool.search([('state', '=', 'open'), ('partner_id', '=', record.customer_id.id)])

unpaid_list_ids = fields.One2many("account.invoice", string="Customer Debt List", compute="_get_unpaid_invoices")