Unique validation error with base_field of a django array field during update

Viewed 10

I want the phone numbers inside my phones field to be unique across all customers. it works fine during creating a new customer, But during update i got a unique validation error.

def prevent_replicated_phone(phone):
    # Count all customers which own the same number
    phone_holders = Customer.objects.filter(phones__contains=[phone]).count()
    if phone_holders > 0 :
        raise ValidationError(
            f'The phone number: {phone} already exist',

class Customer(models.Model):
    def __str__(self) :
        return self.customer_name


    id = models.BigAutoField(primary_key=True)
    customer_name = models.CharField(max_length=100, null=False, blank=False, unique=True)
    phones = ArrayField(models.CharField(max_length=10, 
                        validators=[validate_phone_number, prevent_replicated_phone]), 
                                        default=list, null=True, blank=True)
    customer_type = models.CharField(max_length=10,default='patient', choices=CUSTOMER_TYPE)

   

Let say i have a customer hold the phones ['0795364588'].

I send a put request to update the customer phones with new array ['0795364588', '0668123544'].

Updating the customer phones with postman

How i can solve this.

0 Answers
Related