python - 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)
解决方案
我通过显式键入使用管道修复记录来解决此问题。使用没有@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))
推荐阅读
- sql - 插入新创建的列
- python - DataClassJsonMixin.from_dict 将嵌套 dict 保留为 dict
- swift - 如何使用 SwiftUI 和 CoreData (@FetchRequest) 对动态列表排序进行动画处理
- flutter - Flutter Web 中的 OAuth2?
- media-source - Mediasource 支持 H265 吗?
- nativescript - iOS14 和 Xcode12 的 Nativescript 问题
- r - 提取节点集合的邻居名称作为列表
- python - 如何停止屏幕滚动并开始移动角色
- sql - Postgresql,一个函数调用另一个函数并向调用者获取 2 个输出值
- c - 从一组 8 位数字中解码 11 位数字