Django IntegerField返回长 [英] Django IntegerField returning long

查看:92
本文介绍了Django IntegerField返回长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么,当我从db中获取一个字段时,它定义为$ code> models.IntegerField 在models.py中我得到 long 而不是 int eg我有一个模型 EventSchedule

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

在db中是

  mysql>描述t_event_schedule; 
+ ------------------------ + ------------- + ------ + ----- + --------- + ------- +
| Field |类型|空|关键|默认|额外|
+ ------------------------ + ------------- + ------ + ----- + --------- + ------- +
|月_day | int(11)| NO | | 1 | |
...

但是当我创建一个对象,并从db取回值是 long

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

我正在使用

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

'

解决方案

底层的$ code> dbapi 处理程序的副作用,它为大多数事物返回 long

 >>>导入MySQLdb 
>>> db = MySQLdb.connect(db =test)
>>>> c = db.cursor()
>>> c.execute(Select 1)
1L
pre>

大多数用途的区别是化妆品。从一个驱动程序到另一个驱动程序有细微差别,例如 sqlite3 对于同一个查询,返回long:

 >>> import sqlite3 
>>> db = sqlite3.connect(:memory:)
>>> c = db.cursor()
>>> c.execute (Select 1)
< sqlite3.Cursor对象在0x7f2c425ae9d0>
>>> c.execute(选择1)。 fetchone()
(1,)


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

'

解决方案

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,)

这篇关于Django IntegerField返回长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆