美文网首页
解决django.db.utils.DataError: (1

解决django.db.utils.DataError: (1

作者: 哈林的生活笔记 | 来源:发表于2017-03-30 17:57 被阅读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]
    

    相关文章

      网友评论

          本文标题: 解决django.db.utils.DataError: (1

          本文链接:https://www.haomeiwen.com/subject/gjepottx.html