django 报错:ValueError: The database backend does not accept 0 as a value for AutoField.

错误原因:
在创建一个model时,其中一个字段你用了别一个model的做为外键,并且给这个外键设置了一个默认值 ,而这个默认值不适用于键model中自动创建

再找一下报错的脚本:

  Applying oiarpt.0004_auto_20170822_1419... OK
  Applying oiarpt.0005_auto_20170822_1652...Traceback (most recent call last):
  File "manage.py", line 22, in 
    execute_from_command_line(sys.argv)
  File "D:\python3.6\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line

由上面的信息可以看出在执行:Applying oiarpt.0005_auto_20170822_1652…Traceback (most recent call last):时报错的:
打开项目\oia_report\oiarpt\migrations 找到对应的文件可以看到:

field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='oiarpt.Product'),

问题就出在这里:

解决方法:
删除0005_auto_20170822_1652.py
然后重新执行:
python manage.py makemigrations
python manage.py migrate
此时就不会报错了

你可能感兴趣的:(python报错集合)