Skip to Content
Menú
This question has been flagged
2 Respostes
3401 Vistes

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

Avatar
Descartar
Autor

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

Best Answer

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

Avatar
Descartar
Autor Best Answer

Thanks for your help. 


It's working as  expected.

Avatar
Descartar
Autor

Please help me ~~~

Related Posts Respostes Vistes Activitat
0
de juny 21
2702
1
de nov. 20
4531
2
de juny 20
3267
2
de març 24
5744
1
de jul. 23
1734