Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
577 Weergaven



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


Avatar
Annuleer
Beste antwoord

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

Avatar
Annuleer
Auteur

Thanks for your help. Its working for me