跳至内容
菜单
此问题已终结
3 回复
13616 查看

Hi,


I'd like to remove IF statement (just under # OVERRIDE) from the following method which is in AccountMove class in Odoo addons:



@api.model_create_multi
def create(self, vals_list):
# OVERRIDE
if any('state' in vals and vals.get('state') == 'posted' for vals in vals_list):
raise UserError(_('You cannot create a move already in the posted state. Please create a draft move and post it after.'))

vals_list = self._move_autocomplete_invoice_lines_create(vals_list)

moves = super(AccountMove, self).create(vals_list)

# Trigger 'action_invoice_paid' when the invoice is directly paid at its creation.
moves.filtered(lambda move: move.is_invoice(include_receipts=True) and move.invoice_payment_state in ('paid', 'in_payment')).action_invoice_paid()

return moves


The problem is when I override the method (with copy paste) and remove if any('state'...., the super statement call once more the create method and set the if statement.


How can I override this method and remove the if statement correctly?


Thanks in advance,

Regards.

形象
丢弃
最佳答案

You cannot do that. You have to go into the base code and comment that out.

The other option you have is to change the state to draft in the vals and call the super method. 
When super return a recordset, just write a state to posted (res.state = 'posted').

形象
丢弃
最佳答案

Hi Benjamin,

If you call super, it will call the create method in AccountMove. Instead of using super() you can use models.Model.create().

Please remove this line from the code

moves = super(AccountMove, self).create(vals_list)

and Add this

return models.Model.create(self, vals_list)

Hope this helps.

形象
丢弃
编写者

Thank you for your reply. It gives me this error:

File "/usr/lib/python3/dist-packages/odoo/addons_adquat/adquat_data_import/models/import_invoices.py", line 169, in custom_create

moves = models.Model.create(vals_list[0])

TypeError: create() missing 1 required positional argument: 'vals_list'

I don't understand because I fill the method with the vals_list (dict element).

Answer updated, please check now

编写者

Yeees I saw last Friday and I noticed I had to add self in the create method, Thank you, you won an upvote :)

The drawback is that it will not call the create() method overrode in the any base / custom modules.

He doesn't want to call super after his modification, this will call the create method directly from ORM and will skip all other create() in the object

can i use this solution on non-orm method like _action_cancel in sale order?

for non-orm methods, you can simply define that function without super() to completely override, simple monkey patching

最佳答案

Hello,

Do monkey patching technic ..!

形象
丢弃
相关帖文 回复 查看 活动
1
1月 22
3806
1
11月 15
6082
0
11月 15
13
1
2月 16
5121
1
1月 19
11463