Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
4040 มุมมอง

When the user is creating a new record in a model(for example creating a new contact) I want to check the uniqueness of a field value just at the moment the field is changed, not after finishing and saving the entire form.


Here is my sample code:

quotation_id = fields.Many2one(
​comodel_name="sale.order",
​string="Quotation ID",
​required=True,
​readonly=True
)
and this is my XML:
		​<field name="quotation_id"/>

I've defined a sql constraint as follow:

​_sql_constraints = [
​ ('quotation_id_uniq', 'UNIQUE (quotation_id)', 'This Quotation Number is asigned to a previous spot. Please choose another one.')
​]

The user can enter an ID in the field.
I want to check this field just as it changes and inform the user if ID is selected before.

How can I do that?


อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Is this only possible via Python code ? isn't that something we can define in Odoo Studio ?

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

You can use SQL Constraints that prevent incorrect data before saving the record. For example:-

barcode=fields.Char()
_sql_constraints = [('barcode_uniq', 'unique(barcode)', "A barcode can only be assigned to one product !")]

You can use the above code in your Python file.

for more reference:-https://www.cybrosys.com/blog/configuration-of-python-and-model-constraints-in-odoo-15

Regards

อวตาร
ละทิ้ง
ผู้เขียน

As I mentioned above the problem is that _sql_constraints doesn't check until saving occurs. I want to check the field at the moment it modifies.

Related Posts ตอบกลับ มุมมอง กิจกรรม
adding multiple _sql_constraints? แก้ไขแล้ว
2
ก.ย. 24
2370
1
ส.ค. 23
2143
1
พ.ค. 23
3587
0
ม.ค. 18
2824
3
ส.ค. 25
2467