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

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 ...

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

@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?

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

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

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

Thanks for your help. 


It's working as  expected.

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

Please help me ~~~

Related Posts ตอบกลับ มุมมอง กิจกรรม
0
มิ.ย. 21
3085
1
พ.ย. 20
4926
2
มิ.ย. 20
3682
_sql_constraints fail to be effective แก้ไขแล้ว
2
มี.ค. 24
6329
1
ก.ค. 23
2195