2017-01-10 62 views
0

这里是我的模型:的Django模型CharField抛出INT()错误()

class MarketData(models.Model): 
    data_source = models.CharField(max_length=100) 

这里就是我一直然后测试

python3 manage.py shell 

的代码,我跑这

from q.models import MarketData 
data = MarketData("Broker") 
data.save() 

其中抛出错误

File "/home/soverton/.local/lib/python3.5/site-packages/django/db/models/fields/init.py", line 946, in get_prep_value return int(value) ValueError: invalid literal for int() with base 10: 'Broker'

我已经运行下面的关系并获取状态OK消息

python3 ./manage.py makemigrations q 

Migrations for 'q': 
    q/migrations/0006_auto_20170110_1911.py: 
    - Alter field data_source on marketdata 

最后,我把那些迁移。

python3 manage.py migrate 
Operations to perform: 
     Apply all migrations: admin, auth, contenttypes, q, sessions 
    Running migrations: 
     Applying q.0006_auto_20170110_1911... OK 

那么当我保存一个int()错误时它应该保存一个char?

我最近重新格式化了项目,从SQLite切换到PostgresSQL并删除了迁移历史记录。数据库有什么奇怪的事情发生吗?

+0

的可能的复制[Django的:ValueError异常:对于int()与底座10无效字面:](http://stackoverflow.com/questions/41463473/django-valueerror-无效文字为int-base-10) – e4c5

回答

0

我并没有直接将字段名称归属于参数。

正确的答案是

data = MarketData(data_source="Broker") 
data.save() 
+1

而原因是有一个隐藏字段'ID',它存储整数主键。 – RemcoGerlich