1

I am not using django form to save data in django backend. model.py

contact_no = models.IntegerField()

views.py

number = "9739111111"
data = Profile(contact_no=number)
data.save()

on localhost its working but on server its not getting saved the contact number number is getting saved but default number is getting saved 2147483647 this number. This number is maximum range of integer.

I dont know how to save this number 9739111111 as it is. Please help me.

Wagh
  • 4,202
  • 5
  • 39
  • 62
  • Try using a char field of 15 or 20 instead of an integer, and do the validation before saving – David Zhou Jul 28 '14 at 17:22
  • on server its not working,,,,,,,, getting default value 2147483647 but on localhost its working using charfield – Wagh Jul 28 '14 at 17:25
  • if you change your models, you might need to re-sync your database to apply the changes – David Zhou Jul 28 '14 at 17:26
  • i did all the things,,,,,,, still same problem,,,,,,,, – Wagh Jul 28 '14 at 17:28
  • There is no reason you should be using an integer (or big integer) field for a phone number. Do you do mathematical operations on the contact number? (c1 + c2)? If no, then think about switching to a charfield. –  Jul 28 '14 at 17:41

1 Answers1

3

You're running into the maximum value for a 32 bit integer within a database. Try switching to a django BigIntegerField if you need to store values that large. Using your stated examples:

models.py

contact_no = models.BigIntegerField()

views.py

number = 97391111111
data = Profile( contact_no = number )
data.save()

Having said that, are you sure you're using the correct field type? If this is a phone number (as the field name suggests), do your requirements allow for you to be country specific? If so, you might want to try the localflavor app to validate and store these values appropriately.

Community
  • 1
  • 1
Thomas
  • 1,402
  • 1
  • 11
  • 14
  • give me the small example for BigInteger here. – Wagh Jul 28 '14 at 17:29
  • @Gaurav Instead of `models.IntegerField()` use `models.BigIntegerField()` – Joren Jul 28 '14 at 17:32
  • See the updated answer. It should be as simple as changing out your field type, as per the official django documentation. – Thomas Jul 28 '14 at 17:32
  • on server how to migrate. I am using sql on dreamhost server. – Wagh Jul 28 '14 at 17:34
  • I tried this BigInteger() field also,,,,,,,,, Its not working,,,,,,, or maybe not getting synced properly,,,, – Wagh Jul 28 '14 at 17:35
  • It sounds like the field types may not be getting synced properly. The underlying database type for a BigIntegerField should be BIGINT(). That's probably the first thing to check. – Thomas Jul 28 '14 at 17:47
  • You would have to use `south` for migrations if you are using django 1.6 or less. Also, If it were me, I would do `CharField`, since I am not going to need a phonenumber as an Integer ever, and handle the phone number validation logic in the forms: https://docs.djangoproject.com/en/1.5/ref/contrib/localflavor/#module-django.contrib.localflavor - Note that this is an external package for django 1.6 – karthikr Jul 28 '14 at 17:50
  • I agree with @karthikr, and was just editing this answer to also suggest the localflavor package as an alternative setup. – Thomas Jul 28 '14 at 17:52