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



@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!")



I want to use api.constrains to check. It's working, but when debug I check id record and I see its still create record (its only hide this record). 

How can not create record when it show an error message?


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

Hi,

Try this code:

_sql_constraints = [

        (

            "phone_owner_uniq",

            "unique (phone, owner)",

            "You cannot have two tenants with the same phone number for the same owner!",

        )

    ]



Hope it helps

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

Thanks for your help. Its working for me