简体   繁体   中英

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

Trying to get the best way to store a phone # in Django. At the monent i'm using Charfield and checking if it's a 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.

I store phone numbers in CharField, and use phonenumbers for validation. In forms I allow the user to enter the number any way he wants and then parse,format and validate it using phonenumbers lib.

You can also use the "local flavors" add-on, which defines custom fields for many different countries. This would be useful if you are expecting to have phone numbers from only one country. Please be aware that the implementation of local flavors changed substantially between Django 1.4 and 1.5, so take care with reading the correct documentation. It was built-in before 1.5, now it's an external package.

For US phone numbers (django 1.4)
For US phone numbers (django 1.5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM