Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
13596 Widoki

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.

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Najlepsza odpowiedź

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.

Awatar
Odrzuć
Autor

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

Autor

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

Najlepsza odpowiedź

Hello,

Do monkey patching technic ..!

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
sty 22
3795
1
lis 15
6062
0
lis 15
13
1
lut 16
5110
1
sty 19
11454