Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
3754 Lượt xem

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thank you Karthikeyan N R, it worked!

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


Ảnh đại diện
Huỷ bỏ

Happy to help, thanks again!

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 3 15
10688
1
thg 7 23
2990
3
thg 11 22
5871
0
thg 8 20
4454
0
thg 11 19
4600