stefanfoulis / django-phonenumber-field

A django model and form field for normalised phone numbers using python-phonenumbers
MIT License
1.48k stars 318 forks source link

Multiple countrycodes in `PhoneNumberField` #573

Closed kmartynski closed 1 year ago

kmartynski commented 1 year ago

Hi, I've been playing around with the library. I couldn't find any information around the web or in documentation. Supposedly I have a model in Django

settings.py

PHONENUMBER_DEFAULT_REGION = "PL"
PHONENUMBER_DEFAULT_FORMAT = "INTERNATIONAL"

models.py

class Client(models.Model):
        phone = PhoneNumberField()

I'm not entirely sure if one might use many country codes, something like this:

class Client(models.Model):
        phone = PhoneNumberField(country_codes=["PL", "DE", "ES"])

Is there any option to accept phone numbers from many countries?

francoisfreitag commented 1 year ago

There is no such option, because a regional phone number can be valid in different region, resulting in ambiguity.

I would rather recommend asking for phone numbers with a country selector (e.g. phonenumber_field.widgets.PhoneNumberPrefixWidget), or a phone number in an international format (region=None).

kmartynski commented 1 year ago

@francoisfreitag thank you for rapid response. So if I understand correctly by the second recommendation I should only stick to: PHONENUMBER_DEFAULT_FORMAT = "INTERNATIONAL"

class Client(models.Model):
        phone = PhoneNumberField(region=None)

And remove PHONENUMBER_DEFAULT_REGION = "PL"

Is that right? :)

francoisfreitag commented 1 year ago

Yes, if you don’t want to interpret national phone numbers in your region, leaving PHONENUMBER_DEFAULT_REGION unspecified is the way to go. The system will then only accept phone numbers in an international format. To facilitate data entry for humans, you can use (or take inspiration from) the PhoneNumberPrefixWidget.

As for the format, INTERNATIONAL is a good choice, it allows for international phone numbers, including phone extensions. https://django-phonenumber-field.readthedocs.io/en/stable/reference.html#phonenumber-db-format