Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
592 Vizualizări



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


Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

Thanks for your help. Its working for me