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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow different region validation in DRF SerializerField from PHONENUMBER_DEFAULT_REGION #427

Closed
jrbenriquez opened this issue Mar 8, 2021 · 1 comment · Fixed by #519

Comments

@jrbenriquez
Copy link

jrbenriquez commented Mar 8, 2021

Hi

Currently in our app we don't use Django Forms, all data goes through serializers and so basing from
The solution provided here

Is it possible to declare the fields for a specific region validation?

i.e. something like:

class ASerializer(ModelSerializer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        fields = getattr(self, "_fields", None)
        try:
            instance = args[0]
        except IndexError:
            instance = None
        if not instance:
           return
        if not hasattr(instance, 'get_country', None):
            return
        country = instance.get_country
        self._fields[field_name] = PhoneNumberField(region=country)

I am trying this approach but currently getting

 File "xxx", line 274, in __init__
    self._fields[field_name] = PhoneNumberField(region=country)
  File "xxx/.pyenv/versions/3.6.11/envs/brokerengine/lib/python3.6/site-packages/rest_framework/fields.py", line 754, in __init__
    super(CharField, self).__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'region'

Looks like custom regions in DRF serializers are not supported yet? (I may have to write additonal code for that)
By doing this will I be able to override the normal validation that happens for the default region?

@the-akpan
Copy link

Any idea if this has been resolved?

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.

2 participants