İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
3924 Görünümler

I am trying to show a custom (warning) message upon clicking confirm button in Invoices (account.move). 

The basic idea is: when user confirms new invoice check condition (is this the first time showing message?) and raise UserError if condition is true otherwise continue and create invoice. 

This works fine, but i have bool field serving as a flag (Was message already shown?) and i need to update this flag (with permanent change) before raising UserError. Unfortunately raising UserError causes database rollback so my update of flag field is always rollbacked.

I came across post on Stack Overflow explaining Odoo cursor and raw SQL queries and it seemed like it might be an answer to my problem but i have no idea how to do it.

I want to update only one field (flag field) and not commit whole record.

Avatar
Vazgeç
En İyi Yanıt

you can make use of the Odoo cr (database cursor) object and execute raw SQL queries. Here's an example of how you can update a single field and avoid the rollback caused by raising a UserError:

from odoo.exceptions import UserError

# ...

def your_confirm_method(self):
# Get the current record's ID
record_id = self.id

# Get the database cursor
cr = self.env.cr

# Update the field directly using a raw SQL query
query = """
UPDATE account_move
SET your_flag_field = true
WHERE id = %s
"""
cr.execute(query, (record_id,))

# Check your condition and raise UserError if needed
if your_condition:
raise UserError("Your warning message")

# Continue with the normal invoice confirmation process
# ...

In the above code, replace your_flag_field with the actual name of your flag field in the account_move table. Make sure to replace your_condition with your actual condition for showing the warning message.

By using the database cursor (cr) and executing a raw SQL query, you can update the specific field directly in the database without relying on the ORM's transaction handling.

Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
1
May 22
6807
0
Haz 21
6350
1
Tem 25
2294
2
Tem 25
7758
2
Tem 25
4181