This question has been flagged
3 Replies
10438 Views

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.

Avatar
Discard
Best Answer

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').

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

Author

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

Best Answer

Hello,

Do monkey patching technic ..!

Avatar
Discard