New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialization #225
Comments
I agree. It should just work out of the box with django rest framework. |
What is your current solution? |
Did anyone find a solution for this? |
The project comes with a DRF field. Are you able to use it for you needs? |
|
I would like to see this work more broadly, but I think it's difficult because a PhoneNumber object may be incomplete or incorrect (meaning not a valid phone number). I use a separate wrapper class to manage the "user version" (raw input text) and the phone object, which supports some normalization and validation checks. more about custom json encoding here: https://stackoverflow.com/questions/18478287/making-object%20-json-serializable-with-regular-encoder |
I'm facing the same issue when I get the NOTE: I'm using the PhoneNumber from serializerfields in my serializers, not the model field... |
Could you please provide an example code of how you have solved the issue? |
I fixed it like this:
|
Thank you @tanwirahmad Here's the complete solution that worked for me from rest_framework.serializers import ValidationError
from phonenumber_field.serializerfields import PhoneNumberField
from phonenumber_field.phonenumber import to_python
class CustomPhoneNumberField(PhoneNumberField):
def to_internal_value(self, data):
phone_number = to_python(data)
if phone_number and not phone_number.is_valid():
raise ValidationError(self.error_messages["invalid"])
return phone_number.as_e164 |
What's the upshot of this thread? Will @shahwan42's fix be merged into the release branch? What do folks recommend? |
Hey, guys, let me share my thoughts... I'm trying to pass invalid value to serializerfields.PhoneNumberField and I expect to get ValidationError. But instead I get TypeError, because "to_python" can't convert int to PhoneNumber. Why is it so: TypeError instead of ValidationError? |
This issue seems to be a collection of problems people encountered over the years by using rest_framework with this library. If does not seem to have an issue in particular that needs addressing. |
The modelfield is not JSON serializable out fo the box. Custom solutions have to be used to make it so or else it raises an error as below 馃憤 :
Suggest it is implemented out of the box for use with Django Rest Framework.
The text was updated successfully, but these errors were encountered: