콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
2561 화면

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
5712
0
5월 23
2579
1
5월 23
2053
1
4월 23
1828
2
12월 19
40956