Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
2429 Lượt xem

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


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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;


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 10 23
5592
0
thg 5 23
2507
1
thg 5 23
1988
1
thg 4 23
1745
2
thg 12 19
40779