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

Hello, i am currently using Odoo 12. we have the sql constraints below:


_sql_constraints = [
  ('check_pr_ref',

                        "unique(project_reference)",
                        'The project reference already exists'),
]

i'd like to add an exception to allow if the project_reference = "PR0000"


thanks in advance,
Anthony.

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

Hi,

 In your model add this function,

from odoo.exceptions import  ValidationError

@api.constrains('project_reference')

    def _check_duplicate(self):

        for each in self:

            if each.project_reference and each.project_reference != 'PR0000':

                domain = [('project_reference', '=', each.project_reference)]

                all_forms = self.search_count(domain)

                if all_forms:

                    raise ValidationError(_('The project reference already exists!'))

Hope it helps,

Thanks

อวตาร
ละทิ้ง
ผู้เขียน คำตอบที่ดีที่สุด

Thank you Karthikeyan N R, it worked!

(sorry i don't have enough Karma to mark your comment as best answer)


อวตาร
ละทิ้ง

Happy to help, thanks again!

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
มี.ค. 15
10689
1
ก.ค. 23
3014
remove time from datetime field odoo 15 แก้ไขแล้ว
3
พ.ย. 22
5884
0
ส.ค. 20
4464
0
พ.ย. 19
4605