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

How to Unique constraint a field in custom model on Odoo Enterprise 12.0 Online?

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

Hi Tameem,

I think you are trying to make the Unique field.

Usually, this is handled by SQL Constraints. In your case, you are using Odoo Online. So we must think differently to make it possible.

Anyway

Try this way. Maybe you will get the result you wish.

  1. Create an Automated action with the following parameters.

    • Model: your custom model

    • Trigger Condition: On Update

    • Action To Do: Execute Python Code

    • In the python code, try this code

new_value = record.YourFieldName
existing_records = env['Your.Model'].search([('YourFieldName', '=', new_value)])
if len(existing_records) > 1:
    raise Warning('Field value must be unique')


Thanks

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

Dear Avinash,

Your answer worked perfectly!

Thank you for your reply.

My pleasure

Hello, do you know if we can manage to do the same using Studio ?

คำตอบที่ดีที่สุด

Hello, Avinash's answer works for me too but instead of having the warning I setted I have an Odoo error about the automated action itself. Do you know how to solve this ?

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
พ.ย. 24
1394
2
ม.ค. 24
4706
3
ก.ค. 25
557
2
ก.ค. 25
3865
2
ก.ย. 21
4861