Anurag Uniyal
Anurag Uniyal

Reputation: 88737

Django IntegerField returning long

I am wondering why, when I get a field from db which is defined as models.IntegerField in models.py I am getting long instead of int e.g. I have a model EventSchedule

class EventSchedule(models.Model):
    monthly_day = models.IntegerField(default=1)
    ...

in db it is

mysql> describe t_event_schedule;
+------------------------+-------------+------+-----+---------+-------+
| Field                  | Type        | Null | Key | Default | Extra |
+------------------------+-------------+------+-----+---------+-------+
| monthly_day            | int(11)     | NO   |     | 1       |       |
...

but when I create an object, and retrieve back value from db it is long

>>> e = EventSchedule()
>>> e.monthly_day
1
>>> e.save()
>>> e2 = EventSchedule.objects.get(id=e.id)
>>> e2.monthly_day
1L

I am using

>>> django.VERSION
(1, 2, 1, 'final', 0
>>> platform.python_version()
'2.6.5

'

Upvotes: 3

Views: 1805

Answers (1)

SingleNegationElimination
SingleNegationElimination

Reputation: 156158

this is probably a side effect of the underlying dbapi handler, which returns long for most everything:

>>> import MySQLdb
>>> db=MySQLdb.connect(db="test")
>>> c = db.cursor()
>>> c.execute("Select 1")
1L

The difference for most uses is cosmetic. There are subtle differences from one driver to another, for instance sqlite3 does not return long for this same query:

>>> import sqlite3
>>> db = sqlite3.connect(":memory:")
>>> c = db.cursor()
>>> c.execute("Select 1")
<sqlite3.Cursor object at 0x7f2c425ae9d0>
>>> c.execute("Select 1").fetchone()
(1,)

Upvotes: 4

Related Questions