What's the best way to store Phone number in Django models

后端 未结 8 2071
情歌与酒
情歌与酒 2020-11-27 09:12

I am storing a phone number in model like this:

phone_number = models.CharField(max_length=12)

User would enter a phone number

相关标签:
8条回答
  • 2020-11-27 09:47

    Use django-phonenumber-field: https://github.com/stefanfoulis/django-phonenumber-field

    pip install django-phonenumber-field
    
    0 讨论(0)
  • 2020-11-27 09:53

    It all depends in what you understand as phone number. Phone numbers are country specific. The localflavors packages for several countries contains their own "phone number field". So if you are ok being country specific you should take a look at localflavor package (class us.models.PhoneNumberField for US case, etc.)

    Otherwise you could inspect the localflavors to get the maximun lenght for all countries. Localflavor also has forms fields you could use in conjunction with the country code to validate the phone number.

    0 讨论(0)
提交回复
热议问题