首页 > 解决方案 > Create batch records with Odoo create method

问题描述

I am trying to create multiple invoices from an array of dictionaries with the create values in odoo13. Creating one record at a time is okay but when I try with the batch record I get the error can't adapt to type dict

I have tried looping through the array and create a record for each item in it but this error persists.

I am currently checking on the @api.model_create_multi decorator but haven't grasped it yet fully.

What I want is that for each line in the visa_line(same as order line), to create an invoice from that. Some fields in creating an invoice are missing but that should not be the issue.

When I print the record, in the final function, it prints the duct with values correctly.

Here is my code, thank you in advance

    def _prepare_invoice(self):
        journal = self.env['account.move'].with_context(
            default_type='out_invoice')._get_default_journal()

        invoice_vals = {
            'type': 'out_invoice',
            'invoice_user_id': self.csa_id and self.csa_id.id,
            'source_id': self.id,
            'journal_id': journal.id,
            'state': 'draft',
            'invoice_date': self.date,
            'invoice_line_ids': []
        }
        return invoice_vals

    def prepare_create_invoice(self):
        invoice_val_dicts = []
        invoice_val_list = self._prepare_invoice()
        for line in self.visa_line:
            invoice_val_list['invoice_partner_bank_id'] = line.partner_id.bank_ids[:1].id,
            invoice_val_list['invoice_line_ids'] = [0, 0, {
                'name': line.code,
                'account_id': 1,
                'quantity': 1,
                'price_unit': line.amount,
            }]
            invoice_val_dicts.append(invoice_val_list)
        return invoice_val_dicts

    @api.model_create_multi
    def create_invoice(self, invoices_dict):
        invoices_dict = self.prepare_create_invoice()
        for record in invoices_dict:
            print(record)
            records = self.env['account.move'].create(record)

标签: pythonodooodoo-12odoo-13

解决方案


我通过显式键入使用管道修复记录来解决此问题。使用没有@api.model_create_multi.

def create_invoice(self):
   invoices_dict = self.prepare_create_invoice()
   for record in invoices_dict:
       records = self.env['account.move'].create(dict(record))

推荐阅读