10

In my models I need to store a mobile number in the following format 447182716281. What field should I use? Does Django have anything to support this?

example

 mobile = models.IntegerField(max_length=12)
apomene
  • 14,282
  • 9
  • 46
  • 72
MarkO
  • 775
  • 2
  • 12
  • 25

5 Answers5

5

Phone numbers must be CharFields. Integer field will not preserve leading 0, +, and spacing.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
3

There is a regex field in form validation. In model use just CharField.

msvana
  • 31
  • 2
3

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

PepperoniPizza
  • 8,842
  • 9
  • 58
  • 100
2

On using models.IntegerField(default=0) for larger number it gives error Ensure this value is less than or equal to 2147483647.

So better way to use would be. BigIntegerField A 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The default form widget for this field is a TextInput.

Nitish Kumar Pal
  • 2,738
  • 3
  • 18
  • 23
0

with PostgreSQL IntegerField is not working properly so it's better to use CharField with Django.

Number=models.CharField(max_length=12)
DmitryArc
  • 4,757
  • 2
  • 37
  • 42