使用python代码自动注册付款会引发错误

问题描述

这是我用来注册付款的代码

Payment1 = self.env['account.payment'].with_context(default_invoice_ids=[(4,SaleInvoice.id,False)])
payment = Payment1.create({
    'payment_date': SaleInvoice.date,'payment_method_id': 1,'payment_type': 'inbound','partner_type': 'customer','communication': SaleInvoice.name,'partner_id': SaleInvoice.partner_id.id,'amount': paymentAmount,'journal_id': default_journal_id.id,'company_id': SaleInvoice.company_id.id,'currency_id': self.env.company and self.env.company.currency_id and self.env.company.currency_id.id,'payment_difference_handling': 'reconcile',# 'writeoff_account_id': self.diff_income_account.id,})
payment.post()

它抛出一个错误

Error: odoo Server Error

res = self._obj.execute(query,params)   ValueError: <class 'psycopg2.IntegrityError'>: "new row for relation "account_move_line" violates check constraint "account_move_line_check_accountable_required_fields"

DETAIL:  Failing row contains (32465,7210,null,20,10,Write-Off,1.000,0.00,1056.04,0.0,f,2021-04-19,414,25,t,2,2021-04-21 05:55:09.266162,null).   " while evaluating   'model.paymentSync()'

解决方法

我已经为这个错误寻找了 4 个小时。因为同样的事情发生在我身上。在这种情况下,当发票有未偿债务需要结算时,它会使用 writeoff_account_id 字段 只需发送相关对象 account.account 的 id

payment = Payment1.create({
   'writeoff_account_id': self.env['account.account'].browse("you id").id,})