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

Create @api.constrains to check: 

_sql_constraints = [
        (
            "phone_owner_uniq",
            "unique (phone, owner)",
            "You cannot have two tenants with the same phone number for the same owner!",
        )
    ]

This is sql.contrains but I want to check with python constrains.

Please help me ...

Ảnh đại diện
Huỷ bỏ
Tác giả

@Kiran K
In odoo, it's working.
But in my app (FE: React native), It show error message, but it still create.
Is there any way to fix it?

Câu trả lời hay nhất

Hi BB,

Try,

    @api.constrains('phone', 'owner')

    def check_phone_owner_uniq(self):

        for record in self:

            duplicate_records = self.search([

                ('phone', '=', record.phone),

                ('owner', '=', record.owner.id),

                ('id', '!=', record.id)

            ])

            if duplicate_records:

                raise ValidationError("You cannot have two tenants with the same phone number for the same owner!")

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

Thanks for your help. 


It's working as  expected.

Ảnh đại diện
Huỷ bỏ
Tác giả

Please help me ~~~

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 6 21
3043
1
thg 11 20
4857
2
thg 6 20
3609
2
thg 3 24
6211
1
thg 7 23
2095