Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The title may have been confusing, but please let me explain:

Currently when I am storing phone number with raw digits like 5554441234. Then in my template I was going to "format" the number into something like 555-444-1234.

I realized in Django's localflavor, there is a US phone number field that checks if a string is in XXX-XXX-XXXX format.

So my question is should I enter the phone number as raw digits then format it in the template or should I enter the phone number in the formatted way using localflavor?

If I choose the latter, would the XXX-XXX-XXXX format restriction apply even to the database API level or in the Django admin page?

like image 900
hobbes3 Avatar asked Mar 30 '12 10:03

hobbes3


People also ask

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

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

Is there a phone number field in Django?

A Django library which interfaces with python-phonenumbers to validate, pretty print and convert phone numbers. python-phonenumbers is a port of Google's libphonenumber library, which powers Android's phone number handling. Included are: PhoneNumber, a pythonic wrapper around python-phonenumbers' PhoneNumber class.

How can we make field required in Django?

Let's try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit. Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.


2 Answers

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

like image 157
Björn Lindqvist Avatar answered Oct 16 '22 23:10

Björn Lindqvist


If you're using forms framework (for example, in the Django admin), the validation of the input will be done on the application level. So, to enter phone number in the form, you have to format it to XXX-XXX-XXXX.

On the database level, it's just a CharField, so database doesn't do any checks for the format.

like image 26
gakhov Avatar answered Oct 17 '22 00:10

gakhov