What's the Best Way to Store a Phone Number in Django Models

What's the best way to store a phone number in Django models?

You might actually look into the internationally standardized format E.164, recommended by Twilio for example (who have a service and an API for sending SMS or phone-calls via REST requests).

This is likely to be the most universal way to store phone numbers, in particular if you have international numbers work with.

  1. Phone by PhoneNumberField

    You can use the phonenumber_field library. It is a port of Google's libphonenumber library, which powers Android's phone number handling. See django-phonenumber-field.

    In the model:

    from phonenumber_field.modelfields import PhoneNumberField

    class Client(models.Model, Importable):
    phone = PhoneNumberField(null=False, blank=False, unique=True)

    In the form:

    from phonenumber_field.formfields import PhoneNumberField
    class ClientForm(forms.Form):
    phone = PhoneNumberField()

    Get the phone as a string from an object field:

    client.phone.as_e164

    Normalize the phone string (for tests and other staff):

    from phonenumber_field.phonenumber import PhoneNumber
    phone = PhoneNumber.from_string(phone_number=raw_phone, region='RU').as_e164
  2. Phone by regexp

    One note for your model: E.164 numbers have a maximum character length of 15.

    To validate, you can employ some combination of formatting and then attempting to contact the number immediately to verify.

    I believe I used something like the following in my django project:

     class ReceiverForm(forms.ModelForm):
    phone_number = forms.RegexField(regex=r'^\+?1?\d{9,15}$',
    error_message = ("Phone number must be entered in the format: '+999999999'. Up to 15 digits is allowed."))

As per jpotter6, you can do something like the following in your models as well:

File models.py:

from django.core.validators import RegexValidator

class PhoneModel(models.Model):
...
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True) # Validators should be a list

Good way to store phone numbers in django models

Use this package https://github.com/stefanfoulis/django-phonenumber-field just install it with pip

Django storing mobile number, what field to use?

I think is a interesting question since it really depends on the problem modeling, CharField works fine, but have a look at this:

ORM tricks

Django models -Best way to store multiple contact numbers

If you are using PostgreSQL you can use ArrayField.

contact_info = ArrayField(models.CharField(max_length=15), blank=True)

Official documentation here. ArrayField

When storing phone numbers in Django, should I store them as an raw digits or use django.contrib.localflavor?

Store them as entered, only strip surrounding white space if necessary. The way a user formats its number stores semantic information that you do not want to lose by normalizing it. In the US the format XXX-XXX-XXXX is very common, but other locales use totally different formats. For example, where I live some common formats are: XX-XXX XXX XX, XXX/XXX XX XX, +46(0)70-XXX XX XX and so on ad nausem.

The number displayed to the user should be in the same format as entered, otherwise he or she will think of it as munged or may not even recognize the number if it is formatted in a different style than the user is used to. See also this discussion: http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=6079&ixReplies=6

What's the recommended way for storing a phone number?

I always use a simple CharField, since phone numbers differ so greatly from region to region and country to country. Some people might even use characters instead of numbers - according to the numeric keyboard on phones.

Maybe adding a Choicefield for country prefix is a good idea, but that is as far as I would go.

I would never check a phone number field for any "invalid" data like dashes, spaces etc, because your users might dislike receiving an error message and because of that do not submit a phone number at all.

After all a phone number will be dialled by a person in your office. And they can - and should - verify the number personally.

Use Phone number Field In AbstractBaseUser Django

You're missing a comma here:

INSTALLED_APPS = [
...
'Accounts.apps.AccountsConfig', # <-- here
'phonenumber_field',
]


Related Topics



Leave a reply



Submit