Skip to content
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

Closed
jerryshikanga opened this issue Aug 5, 2018 · 13 comments
Closed

Serialization #225

jerryshikanga opened this issue Aug 5, 2018 · 13 comments

Comments

@jerryshikanga
Copy link

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 馃憤 :

  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type PhoneNumber is not JSON serializable

Suggest it is implemented out of the box for use with Django Rest Framework.

@stefanfoulis
Copy link
Owner

I agree. It should just work out of the box with django rest framework.

@bukowa
Copy link

bukowa commented Dec 10, 2018

What is your current solution?

@ameya-amazatic
Copy link

ameya-amazatic commented May 27, 2019

Did anyone find a solution for this?

@jdufresne
Copy link
Collaborator

The project comes with a DRF field. Are you able to use it for you needs?

https://github.com/stefanfoulis/django-phonenumber-field/blob/master/phonenumber_field/serializerfields.py

@emilgeorgejames1
Copy link

emilgeorgejames1 commented May 31, 2019

from phonenumber_field.serializerfields import PhoneNumberField

class SignupSerializer(serializers.Serializer):
    mobile_number = PhoneNumberField()

@cgthayer
Copy link

cgthayer commented Nov 4, 2019

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

@shahwan42
Copy link

shahwan42 commented May 7, 2020

I'm facing the same issue when I get the serializer.valdiated_data and try to dump it as JSON...
PhoneNumber class has to be JSON-serializable

NOTE: I'm using the PhoneNumber from serializerfields in my serializers, not the model field...

@olek5andr
Copy link

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

Could you please provide an example code of how you have solved the issue?

@tanwirahmad
Copy link

I fixed it like this:

class PhoneNumberFieldSerializable(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

@shahwan42
Copy link

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

@kohlab
Copy link

kohlab commented Nov 28, 2020

What's the upshot of this thread? Will @shahwan42's fix be merged into the release branch? What do folks recommend?

@rudneff13
Copy link

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?

@francoisfreitag
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.