Skip to Content
Menu
This question has been flagged
2 Replies
4009 Rodiniai

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

Portretas
Atmesti
Autorius

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

Portretas
Atmesti
Autorius Best Answer

Thanks for your help. 


It's working as  expected.

Portretas
Atmesti
Autorius

Please help me ~~~

Related Posts Replies Rodiniai Veikla
0
birž. 21
3058
1
lapkr. 20
4900
2
birž. 20
3638
2
kov. 24
6264
1
liep. 23
2147