Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
4082 Näkymät

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
Hylkää
Tekijä

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

Paras vastaus

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
Hylkää
Tekijä Paras vastaus

Thanks for your help. 


It's working as  expected.

Avatar
Hylkää
Tekijä

Please help me ~~~

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
0
kesäk. 21
3080
1
marrask. 20
4923
2
kesäk. 20
3665
2
maalisk. 24
6315
1
heinäk. 23
2189