diff --git a/phonenumber_field/serializerfields.py b/phonenumber_field/serializerfields.py index d0a0a380..2502a57a 100644 --- a/phonenumber_field/serializerfields.py +++ b/phonenumber_field/serializerfields.py @@ -9,7 +9,8 @@ class PhoneNumberField(serializers.CharField): default_error_messages = {"invalid": _("Enter a valid phone number.")} def to_internal_value(self, data): - phone_number = to_python(data) + str_value = super().to_internal_value(data) + phone_number = to_python(str_value) if phone_number and not phone_number.is_valid(): raise ValidationError(self.error_messages["invalid"]) return phone_number diff --git a/tests/test_serializers.py b/tests/test_serializers.py new file mode 100644 index 00000000..55f7a989 --- /dev/null +++ b/tests/test_serializers.py @@ -0,0 +1,13 @@ +from django.test import SimpleTestCase +from rest_framework import serializers + +from phonenumber_field.serializerfields import PhoneNumberField + + +class PhoneNumberFieldTest(SimpleTestCase): + def test_int(self): + class PhoneNumberSerializer(serializers.Serializer): + phone = PhoneNumberField() + + s = PhoneNumberSerializer(data={"phone": 1}) + self.assertIs(s.is_valid(), False)