假设我有一个这样的词典列表:
[{'amount': 42140.0, 'name': 'Payment', 'account_id_credit': 385, 'type': u'expense', 'account_id_debit': 476},
{'amount': 43926.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'payable', 'account_id_debit': 641},
{'amount': 3800.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'expense', 'account_id_debit': 476},
{'amount': 46330.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'expense', 'account_id_debit': 476},
{'amount': 67357.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'payable', 'account_id_debit': 323},
{'amount': 26441.0, 'name': 'Payment', 'account_id_credit': 695, 'type': u'expense', 'account_id_debit': 476} ... ]
Run Code Online (Sandbox Code Playgroud)
我想将字典合并在一起,以便关键的"金额"将是所有amounts字典的总和,其中account_id_credit和account_id_debit是相同的,但只有type这些是expense.其他types应该保持原样.
最好的方法是什么?
我正在尝试创建一个选择字段,其中的值应该动态计算。声明是:
payment_mode = fields.Selection('_compute_selection', string="Payment mode", default="cash")
Run Code Online (Sandbox Code Playgroud)
和功能:
@api.model
def _compute_selection(self):
context = dict(self._context or {})
active_ids = context.get('active_ids')
active_model = context.get('active_model')
invoices = self.env[active_model].browse(active_ids)
type_payment = MAP_INVOICE_TYPE_PARTNER_TYPE[invoices[0].type]
if type_payment == "expense":
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal'),
('transfer', 'Bank transfer')]
else:
payment_mode = [('cash', 'Cash'),
('transfer', 'Bank transfer')]
else:
if active_model == "account.invoice":
payment_mode = [('cash', 'Cash'),
('idoklad', 'Internal')]
else:
payment_mode = [('cash', 'Cash')]
return payment_mode
Run Code Online (Sandbox Code Playgroud)
所见声明:
<field name="payment_mode" widget="radio" />
Run Code Online (Sandbox Code Playgroud)
但是,在模块升级时我收到此错误:
File "/home/openuser/erp10/odoo/models.py", line …Run Code Online (Sandbox Code Playgroud)