2012-02-29 8 views
0

我刚刚写了一个小应用程序,用Django.I从我的mysql数据库中存储单词。从文本文件中读取数据的组织非常好,如下所示: 文本文件就像这样的:Django的mysql保存对象时发出警告

DELUGE 
DELUSION 
DELVE 
DEMAGOGUE 
DEMANDING 
DEMOLITION 
DEMONSTRATE 
DEMORALIZE 
DEMOTIC 
DEMUR 
DENIGRATE 
DENOUEMENT 
DENOUNCE 
DENT 
DENUDE 
DEPLETE 
DEPLORE 
DEPLOY 

然后我使用open( 'thefile')从中读取日期readlines方法是这样的:

for line in open('/home/jacos/sorted-gre.txt').readlines(): 
...  if line: 
...   p = Word(word_spelling = line) 
...   p.save() 

的word_spelling字段是主键。 然后来到这样的警告: 回溯(最近通话最后一个):

File "<console>", line 4, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 460, in save 
    self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 553, in save_base 
    result = manager._insert(values, return_id=update_pk, using=using) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 195, in _insert 
    return insert_query(self.model, values, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1436, in insert_query 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 791, in execute_sql 
    cursor = super(SQLInsertCompiler, self).execute_sql(None) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 735, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 34, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 86, in execute 
    return self.cursor.execute(query, args) 
    File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 176, in execute 
    if not self._defer_warnings: self._warning_check() 
    File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 92, in _warning_check 
    warn(w[-1], self.Warning, 3) 
    Data truncated for column 'word_spelling' at row 1 

其结果是,只有这些话被存储在mysql.I'd部分想知道这是为什么。 谢谢。

+0

您错过了回溯中的最后一行......显示确切地说出现了哪个错误的行。你可以添加这些吗? – 2012-02-29 07:35:18

+0

@DavidWolever警告:第1行的列'word_spelling'的数据被截断。 – Gnijuohz 2012-02-29 07:37:38

+1

您的模型如何定义? – StefanoP 2012-02-29 08:33:14

回答

0

CharFields有一个max_length属性。当你为Word.object.get(pk=1).word_spelling生成数据库 时,你设置了什么?

与您的警告没有任何关系,但 建议关闭文件或使用with声明打开文件。

with open('/home/jacos/sorted-gre.txt') as f: 
    for line in f.readlines(): 
     if line: 
      p = Word(word_spelling = line) 
      p.save() 
+0

哎呀的模型定义。我忘记了max_length已被设置为20,并且我发现有更长的单词(错误地)。所以问题解决了。感谢您的帮助。 – Gnijuohz 2012-02-29 13:25:29