Categories
Python Answers

How to store a phone number in Python Django models?

Spread the love

To store a phone number in Python Django models, we can use the phonenumber_field libraru.

To install it, we run

pip install django-phonenumber-field[phonenumbers]

Then in settings.py, we add

INSTALLED_APPS = [
    ...
    'phonenumber_field',
    ...
]

to add 'phonenumber_field' into INSTALLED_APPS.

And then in our model, we add

from phonenumber_field.modelfields import PhoneNumberField

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

to add the phone PhoneNumberField to add a phone number field.

Then we create a form with

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

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *