解决django.db.utils.DataError: (1
2017-03-30 本文已影响0人
哈林的生活笔记
问题
利用pycharm在Django项目创建超级管理员时候,出现错误
Username: admin
Email address: admin@test.com
Warning: Password input may be echoed.
Password: zhang123
Warning: Password input may be echoed.
Password (again): zhang123
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 43, in <module>
run_module(manage_file, None, '__main__', True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
' ' ' '
' ' ' '
self.errorhandler(self, exc, value)
File "/Users/Southernsky/virtualenv/workspace2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")
分析
从最后一句可以发现,错误是由于‘gender’长度过长
解决方法
mac上pycharm全局搜索快捷键command + shift +R 搜索gender
可见
gender = models.CharField(max_length=5,choices=(('male',u'男'),('famale',u'女')),default='female')
female的长度为6,而max_length=5,跟着视频无脑码代码没有思考,这里将5改为10即可。
然后
makemigrations [appname]
migrate [appname]