跳至内容
菜单
此问题已终结
3 回复
2379 查看

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


形象
丢弃
最佳答案

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

形象
丢弃
最佳答案

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

形象
丢弃
最佳答案

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;


形象
丢弃
相关帖文 回复 查看 活动
2
10月 23
5505
0
5月 23
2450
1
5月 23
1916
1
4月 23
1725
2
12月 19
40700