Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
3 Antwoorden
2432 Weergaven

Hi all


I have checked the unique phone number validation with @api.constrains


​class ResPartnerInherits(models.Model):

_inherit = 'res.partner'


​@api.constrains('phone')

​ def _mobile_filed_validation(self):


​ ​ for rec in self:


​ ​ ​ if rec.phone:


​ ​ ​ ​ existing_partner = self.search([('phone', '=', rec.phone)])


​ ​ ​ if existing_partner:


​ ​ ​ ​raise ValidationError("A contact with the same phone number already exists.")



Im getting the output a validation error "A contact with the same phone number already exists."  even though when I enter a unique phone number




What I have rectified here is even if i enter a unique phone number which doesn't exist in the database im getting the validation error and when I try to debug the code it gives me a res partner object (ex res.partner(930)) which doesnt exist in the database




Can anyone assist me in rectifying where the res_partner object with the id is getting stored 


1. Cache/cookies/session/local storage of the browser


2. In some temporary table in the database


Avatar
Annuleer
Beste antwoord

HI,
You can modify your code in two ways:

1. Using search count check if there is two partners with this phone number and if yes, raise validation error


existing_partner = self.search_count([('phone', '=', rec.phone)])

if existing_partner > 1:


2. Exclude the current record from the search by adding domain [('id', '!=', rec.id)]


Thanks

Avatar
Annuleer
Beste antwoord

Hi,

@api.constrains('phone')
    def _mobile_field_validation(self):
        for rec in self:
            if rec.phone:
                duplicate_partners = self.env['res.partner'].search([
                    ('phone', '=', rec.phone),
                    ('id', '!=', rec.id)  # Exclude the current record
                ])
                if duplicate_partners:
                    raise ValidationError(
                        "A contact with the same phone number already exists.")


Hope it helps

Avatar
Annuleer
Beste antwoord

object res_partner is stored in Database, use pgAdmin or any other PostgreSQL database gui client and execute the following SQL statement

Select * from res_partner order by id;


Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
2
okt. 23
5595
0
mei 23
2509
1
mei 23
1992
1
apr. 23
1746
2
dec. 19
40783